Sqlite集锦

用SqliteDatab查询时,query函数第二个函数的new String[]{}个数决定了查询到的Cursor的列数,这与后面的c.getInt(columnIndex)有很大关系。

public void Acctitem_newitem(String text, int type) {
  Cursor c = db.query(TABLE_ACCTITEM, new String[] { "max(_id)","PID","NAME" },
    "_id is not null and _id<9998", null, null, null, null);
  c.moveToFirst();
  if (c.getCount() == 0) {
   Log.d("TAG", "没有id+1的项");
  } else {
   Log.d("TAG",
     "有id+1的项:" + c.getInt(0) + ", "
       + c.getInt(c.getColumnIndex("PID")) + ", "
       + c.getString(c.getColumnIndex("NAME")) + ", ");
  }
  
  int maxid = c.getInt(0)+1;
  String sql = "insert into " + TABLE_ACCTITEM + " values (" + maxid
    + "," + type + ",'" + text + "')";
  Log.d("TAG",c.getCount()+", "+c.getInt(0)+"\n,"+sql);
  db.execSQL(sql);
 }

你可能感兴趣的:(Sqlite集锦)