2017-07-07 41 views
-1

當我嘗試檢查,如果列中存在或不壓傷這個錯誤的程序:或檢查列中存在的不

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 - >https://pastebin.com/94pkJFLd

Contact.java - >https://pastebin.com/P8DiHbxY

回答

-1

你需要把它改成這樣:

String countQuery = "SELECT * FROM " + TABLE_CONTACTS + " WHERE pid = " + id + " AND ptype = '" + type+"'"; 

這只是在您的類型字符串周圍放置單引號來表示它是數據庫的字符串值

所以查詢會是這樣的: SELECT * FROM savedproducts其中pid = 1個AND p型= '香水'

不同的是p型=香水VS p型= '香水' 在實際查詢

你並沒有在你的類型字符串中加上單引號,所以數據庫認爲你試圖獲得一個列,當它實際上試圖告訴它一個ptype值時

+0

它的工作原理,非常感謝。 – Ghadeer