SQLiteException:靠近“=”:语法错误(代码 1)
SQLiteException: near "=": syntax error (code 1)
我正在尝试从我的 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)
这是我的数据源class:
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);
}
}
在构建选择标准时,您还需要提供参数占位符 ?
。
您应该使用 = ?
而不是简单地使用 =
。
我正在尝试从我的 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)
这是我的数据源class:
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);
}
}
在构建选择标准时,您还需要提供参数占位符 ?
。
您应该使用 = ?
而不是简单地使用 =
。