0

这是我创建的表:

public void onCreate(SQLiteDatabase db) {

    String insertNewFormDetails = "create table if not exists " + TABLE_NAME + " ( " + BaseColumns._ID + " integer primary key autoincrement, " 
                                                            + NAME + " text not null, "
                                                            + SCHOOL + " text not null, "
                                                            + CURRENTDATE + " text not null, "
                                                            + FORMTYPE + " text not null);";
    // Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
    db.execSQL(insertNewFormDetails);
}

所以现在这个主键具有自动增量。我如何实际获取该列的内容?

我现在正在为UPDATE查询做什么。

DBAddForm dbOpener2 = new DBAddForm(this);
    SQLiteDatabase sqliteDatabase2 = dbOpener2.getReadableDatabase();
    ContentValues contentValues2 = new ContentValues();

    String selectQuery = "SELECT  * FROM " + "FormDetails";
    SQLiteDatabase db = dbOpener2.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
                contentValues2.put("school",school);
                sqliteDatabase2.update("FormDetails", contentValues2, cursor.getString(0) + " = " +primaryKey , null);
        } while (cursor.moveToNext());
    }

但是,它现在所做的是更新第一列中的所有行。我只希望它在等于primaryKey. 请指教?谢谢!我对所有的尝试感到非常困惑。

4

3 回答 3

0

尝试这个,

删除"FormDetails"并替换为TABLE_NAME

然后删除school并替换为SCHOOL.

select查询返回所有记录。我认为Cursor.getString(0)总是获得第一个价值。并primary key通过打印检查您是否正确更新。

而不是do While循环尝试for循环

for(int i=0;i<cursor.getCount();i++)
{
//Now check your cursor value with the primary key. It may work
Use this cursor.getString(i)
}
于 2013-01-08T09:06:47.897 回答
0

感谢大家!我的回答根据建议进行了调整。现在可以了。

DBAddForm dbOpener2 = new DBAddForm(this);
    SQLiteDatabase sqliteDatabase2 = dbOpener2.getReadableDatabase();
    ContentValues contentValues2 = new ContentValues();

    String selectQuery = "SELECT _id FROM " + DBAddForm.TABLE_NAME;
    SQLiteDatabase db = dbOpener2.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
                contentValues2.put("SCHOOL",school);
                contentValues2.put("NAME", name);
                //update query
                sqliteDatabase2.update("FormDetails", contentValues2, "_id" +  " = " + primaryKey  , null);
        }
        while (cursor.moveToNext());
    }
    sqliteDatabase2.close();
}
于 2013-01-09T02:45:13.757 回答
0

首先,您应该使用 SCHOOL而不是"school". 而不是TABLE_NAME.Youre"FormDetails"正在寻找硬编码字符串的麻烦。

然后,打印或记录 和 的Cursor.getString(0)primaryKey。然后您可以自己调试问题

于 2013-01-08T08:22:47.277 回答