当我尝试检查列是否存在时,它会破坏应用程序并显示以下错误:
Caused by: android.database.sqlite.SQLiteException: no such column: perfumes (code 1): , while compiling: SELECT * FROM savedproducts WHERE pid = 1 AND ptype = perfumes
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
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:1438)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1377)
at app.botikaty.com.DatabaseHandler.CheckIfExist(DatabaseHandler.java:137)
at app.botikaty.com.ViewProductActivity.onCreate(ViewProductActivity.java:80)
我尝试过的是这段代码(DatabaseHandler.java:137):
int CheckIfExist(String id,String type) {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS + " WHERE pid = " + id + " AND ptype = " + type;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
return cursor.getCount();
}
ViewProductActivity.java:80
int issaved = dbHandler.CheckIfExist(theproductid,theproducttype);
if (issaved != -1){
savebtn.setLiked(true);
}else{
savebtn.setLiked(false);
}
我希望问题清楚明白,可以做什么?
我使用的一些文件:
DatabaseHandler.java - &gt; https://pastebin.com/94pkJFLd
Contact.java - &gt; https://pastebin.com/P8DiHbxY
答案 0 :(得分:-1)
您需要将其更改为:
String countQuery = "SELECT * FROM " + TABLE_CONTACTS + " WHERE pid = " + id + " AND ptype = '" + type+"'";
这只是在您的类型字符串周围加上单引号,表示它是数据库的字符串值
所以查询将如下所示: SELECT * FROM savedproducts WHERE pid = 1 AND ptype ='perfumes'
实际查询中的差异是ptype = perfumes vs ptype ='perfumes'
你没有在你的类型字符串周围放置单引号,所以数据库认为你试图获取一个列,而实际上是在试图告诉它ptype的值