SQLiteException:near“=”:语法错误(代码1)

时间:2016-06-20 13:02:54

标签: android sqlite android-sql

我正在尝试从我的sqlite数据库中读取一个对象,但是这样做会出现这样的错误:

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 个答案:

答案 0 :(得分:1)

在构建选择条件时,您还应该提供参数占位符?

您应该使用= ?而不是简单地使用=