【发布时间】:2019-01-26 16:29:23
【问题描述】:
我想在 Java 中使用 SQL Lite 创建一个 SQL 数据库。
String createTable = "CREATE TABLE " + TABLE_NAME
+ " (ID INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL2 +" TEXT, "
+ COL3 +" INTEGER"
+ ")";
我有这个 getTime 方法:
public Cursor getTime(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL3 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
还有一种更新时间的方法:
public void updateTime(int id, String name, int time) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL3 +
" = '" + time + "' WHERE " + COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + name + "'";
Log.d(TAG, "updateName: query: " + query);
Log.d(TAG, "updateName: Setting time to " + time);
db.execSQL(query);
}
我像这样使用 getTime():
Cursor test = mDatabaseHelper.getTime(selectedName);
int timeTest = test.getInt(0); <-- Error here!
System.out.print(timeTest);
由于某种原因,任何事情都无法按照我想要的方式工作。 感谢您的帮助:D
【问题讨论】:
-
请在此处解释“不工作”对您意味着什么。
-
光标测试 = mDatabaseHelper.getTime(selectedName); int timeTest = test.getInt(0);
-
如果不使用命令参数,至少要转义单引号以防止SQL Injection。
" = '" + name.Replace("'", "''") + "'".