2016-06-20 116 views
-2

我想從我的SQLite數據庫讀取單個對象,但我得到這個錯誤這樣做:SQLiteException:近「=」:語法錯誤(代碼1)

android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: SELECT id FROM Property WHERE id = 
                       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
                       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887) 
                       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498) 
                       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:1316) 
                       at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163) 
                       at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034) 
                       at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1240) 
                       at xdesign.georgi.espc_retrofit.Database.EspcItemDataSource.getPropertyItemById(EspcItemDataSource.java:117) 
                       at xdesign.georgi.espc_retrofit.EspcJobSheculerService.onResponse(EspcJobSheculerService.java:84) 

這是我的DataSource類:

public class EspcItemDataSource { 
//The context of the class 
private Context mContext; 
//string tag used for debugging purposes 
private static final String TAG = EspcItemDataSource.class.getSimpleName(); 
// SQLiteDatabase object 
private SQLiteDatabase mDatabase; 
// RssFeedSQLiteHelper object 
private EspcSQLiteHelper mDbHelper; 
// string array of all database columns 
private String[] allColumns = { 
     EspcSQLiteHelper.COLUMN_ID, 
     EspcSQLiteHelper.COLUMN_PRICE, 
     EspcSQLiteHelper.COLUMN_ADDRESS, 
     EspcSQLiteHelper.COLUMN_USER_ID, 
     EspcSQLiteHelper.COLUMN_LAST_UPDATED 
}; 

/** 
* Constructor of the class 
* 
* @param context normally an Activity object is passed as context 
*/ 
public EspcItemDataSource(Context context) { 
    //instanciate class fields... 
    mDbHelper = new EspcSQLiteHelper(context); 
    mContext = context; 
} 

// open the database for writing... 
public void open() throws SQLException { 
    mDatabase = mDbHelper.getWritableDatabase(); 
} 

/** 
* Method that closes the database for future manipulations 
*/ 
public void close() { 
    mDbHelper.close(); 
} 


public Property getPropertyItemById(int id){ 
    Cursor cursor = mDatabase.query(EspcSQLiteHelper.TABLE_NAME, new String[] { EspcSQLiteHelper.COLUMN_ID}, EspcSQLiteHelper.COLUMN_ID + " = ", 
      new String[] { String.valueOf(id) }, null, null, null, null); 
    if (cursor != null) 
     cursor.moveToFirst(); 

    return generateObjectFromCursor(cursor); 
} 


public Property generateObjectFromCursor(Cursor cursor) { 
    //check if the cursor is null 
    if (cursor == null) { 
     //... return 
     return null; 
    } 

    Property propertyItem = new Property(); 
    //set up the object using the cursor object... 
    propertyItem.setId(cursor.getInt(cursor.getColumnIndex(EspcSQLiteHelper.COLUMN_ID))); 
    propertyItem.setUserID(cursor.getInt(cursor.getColumnIndex(EspcSQLiteHelper.COLUMN_USER_ID))); 
    propertyItem.setAddress(cursor.getString(cursor.getColumnIndex(EspcSQLiteHelper.COLUMN_ADDRESS))); 
    propertyItem.setPrice(cursor.getString(cursor.getColumnIndex(EspcSQLiteHelper.COLUMN_PRICE))); 
    propertyItem.setLastUpdated(cursor.getString(cursor.getColumnIndex(EspcSQLiteHelper.COLUMN_LAST_UPDATED))); 
    //return the object 
    return propertyItem; 
} 

我得到當我試圖讓這裏的屬性錯誤:

for (Property p : response.body()) { 
      // Store the property in the database... 
      // TODO DO SYNC LOGIC HERE IN A worker thread 
      Log.e(TAG, "Property with address: " + p.getAddress()); 


      if(mPropertyItemDataSource.ifExistsLocally(p)){ 
       // entry exists locally. Check if needs updating... 
       Property localProperty = mPropertyItemDataSource.getPropertyItemById(p.getId()); 
       Log.d(TAG,localProperty.toString()); 

      }else{ 
       // entry does not exists locally. Create it... 
       mPropertyItemDataSource.createPropertyItem(p); 
      } 
     } 
+1

我猜它是'=?'而不是'='...... – Opiatefuchs

+0

什麼樣的數據類型是ID? –

回答

1

我們希望你提供的對在建立您的選擇標準時也可以使用計算器佔位符?

您應該使用= ?而不是簡單地使用=

+0

太簡單了!謝謝! –

相關問題