我收到一个奇怪的错误:
E/SQLiteLog﹕ (1) no such column: Test
06-22 01:51:20.006 15089-15089/com.almas.mehr.sms E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: no such column: Test (code 1): , while compiling: SELECT * FROM groups WHERE title = Test
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1090)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:663)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1420)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1359)
at com.almas.mehr.databases.MainDB.isSameGroup(MainDB.java:93)
at com.almas.mehr.submain.Groups.onClick(Groups.java:99)
at android.view.View.performClick(View.java:4354)
数据库类:
//database name
private final static String DATABASE_NAME = "almasmehrsms" ;
//tables name
private final String GROUPS = "groups";
private final String NOTIFICATIONS = "notifications";
//columns of groups table
private final String ID_GROUPS = "_id";
private final String TITLE_GROUPS = "title";
private final String DATE_GROUPS = "gdate";
//create groups table
private final String CREATE_GROUPS_TABLE = "CREATE TABLE " + GROUPS + "("
+ ID_GROUPS + " INTEGER PRIMARY KEY AUTOINCREMENT," + TITLE_GROUPS + " TEXT," +
DATE_GROUPS + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP" + ")";
public MainDB(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_GROUPS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i2) {
db.execSQL("DROP TABLE IF EXISTS " + GROUPS);
db.execSQL("DROP TABLE IF EXISTS " + NOTIFICATIONS);
onCreate(db);
}
public Boolean isSameGroup(String title){
Boolean issamegroup = true ;
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM "+ GROUPS +" WHERE "+ TITLE_GROUPS +" = " + title;
Cursor cursor = db.rawQuery(query, null);
cursor.moveToFirst();
if(cursor.getCount() > 0) {
issamegroup = true ;
}else{
issamegroup = false ;
}
return issamegroup ;
}
我写了这个函数来检查值是否重复:
public Boolean isSameGroup(String title){
...
}
例如我EditText
的输入是Test
,我收到此消息错误:
E/SQLiteLog﹕ (1) no such column: Test
答案 0 :(得分:2)
SQL中的字符串文字用单引号('
)表示。没有它们,字符串将被视为对象名称。在这里,您生成一个where子句title = Test
。两者都被解释为列名称,并且查询失败,因为没有列Test
。
要解决此问题,您可以用引号括起Test
:
String query = "SELECT * FROM "+ GROUPS +" WHERE "+ TITLE_GROUPS + " = '" + title + "'";
答案 1 :(得分:1)
将WHERE子句更改为...
...
title = 'test'
它的编写方式是寻找名为Test的列。