android SQLite通过参数查找whereclause

时间:2014-07-07 16:53:03

标签: android database sqlite

我有一个带有两个参数的sqlite类:name和TimesOpened。

我想更新TimesOpened,如何在不知道whereclause但只知道name参数的情况下在sqlite中找到并更新它?

这是我的SQLiteOpenHelper类

    public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "app_name";
public static final String KEY_OPENED = "app_opened";
public static final String DATABASE_NAME = "favorite_apps";
public static final String DATABASE_TABLE ="apps_table";
public static int DATABASE_VERSION = 1;

    private static class Dbhelper extends SQLiteOpenHelper{
public Dbhelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
            KEY_ROWID +" INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_NAME + " TEXT NOT NULL, "+
            KEY_OPENED + " TEXT NOT NULL);"

);
    db.query(DATABASE_TABLE, new String[] { KEY_ROWID,
                    KEY_NAME }, null, null,
            null, null, KEY_NAME+ " ASC");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXITS "+DATABASE_TABLE);
    onCreate(db);
}

该应用程序会自动将项目添加到数据库中,因此我无法告诉每个项目where子句是什么,但我可以告诉其名称。

我试图立即使用的代码更新

    public void find_clause(String appname, int TimesOpened){
    database.execSQL(String.format("UPDATE %s SET %s=%s WHERE %s=%s",
            DATABASE_TABLE,KEY_OPENED,TimesOpened,KEY_NAME,appname));
}

我在这里叫它

     info.find_clause(foregroundTaskPackageName,sharedPrefs.getInt(foregroundTaskPackageName, 0) + 1);

foregroundTaskPackageName是app_name和" sharedPrefs.getInt(foregroundTaskPackageName,0)+ 1)"是time_opened

当我收回数据库时似乎没有任何改变,日志给我这个`

07-07 20:54:21.093  21213-21253/com.assist.me E/SQLiteLog﹕ (1) no such column: com.assist.me


07-07 20:54:27.103  21213-21253/com.assist.me W/System.err﹕ android.database.sqlite.SQLiteException: no such column: com.assist.me (code 1): , while compiling: UPDATE apps_table SET app_opened=126 WHERE app_name=com.assist.me
07-07 20:54:27.103  21213-21253/com.assist.me W/System.err﹕ at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-07 20:54:27.103  21213-21253/com.assist.me W/System.err﹕ at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
07-07 20:54:27.103  21213-21253/com.assist.me W/System.err﹕ at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
07-07 20:54:27.103  21213-21253/com.assist.me W/System.err﹕ at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-07 20:54:27.103  21213-21253/com.assist.me W/System.err﹕ at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-07 20:54:27.103  21213-21253/com.assist.me W/System.err﹕ at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
07-07 20:54:27.103  21213-21253/com.assist.me W/System.err﹕ at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
07-07 20:54:27.103  21213-21253/com.assist.me W/System.err﹕ at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
07-07 20:54:27.103  21213-21253/com.assist.me W/System.err﹕ at com.pin.assistme.favorite_apps.find_clause(favorite_apps.java:30)
07-07 20:54:27.103  21213-21253/com.assist.me W/System.err﹕ at com.pin.assistme.services$ThreadDemo.run(services.java:127)

com.assist.me是应用的包名称更新 正确的代码是

db.execSQL(String.format("UPDATE %s SET %s='%s' WHERE %s='%s'", DATABASE_TABLE,KEY_OPENED,SET_TO_THIS,KEY_NAME,YOUR_NAME));

2 个答案:

答案 0 :(得分:0)

我相信这就是你要找的东西:

db.execSQL(String.format("UPDATE %s SET %s='%s' WHERE %s='%s'",
                DATABASE_TABLE,KEY_OPENED,SET_TO_THIS,KEY_NAME,YOUR_NAME));

将SET_TO_THIS更改为您要设置app_opened的值。

将YOUR_NAME更改为您知道的值(名称参数)。

编辑: 更好的是使用更新方法:

ContentValues content = new ContentValues();
content.put(KEY_OPENED, TimesOpened);

db.update(DATABASE_TABLE, content, KEY_NAME+"=?", new String[] {appname});

答案 1 :(得分:0)

您的日志显示

查询您要执行的内容是

       UPDATE apps_table SET app_opened=126 WHERE app_name=com.assist.me

查询应该是

      UPDATE apps_table SET app_opened=126 WHERE app_name='com.assist.me'

相应地更改您的方法以获取应用名称的单引号并尝试