我收到语法错误代码1并且无法解决它..当我尝试从db获取数据时我得到此错误。我看起来一切似乎都没问题。我正在使用putExtra()和getExtras()来获取变量并按它排序。 这是我的错误代码。 这是我的Dbhandler,光标线上有错误。
private static final String DATABASE_NAME = "Log.db";
//table name
public static final String TABLE_LOGS = "Logs";
//TableLogs column names
public static final String KEY_ID = "id";
public static final String KEY_PLATENUMBER = "platenumber";
public static final String KEY_PLACE = "place";
public static final String KEY_SORT = "sortid";
public static final String KEY_GRADE = "grade";
public static final String KEY_DIAMETER = "diameter";
public static final String KEY_LENGTH = "length";
public static final String KEY_CREATED_AT ="created_at";
public LogsDBHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGS_TABLE = "CREATE TABLE " + TABLE_LOGS + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_CREATED_AT + " TIMESTAMP DEFAULT CURRENT_DATE,"+
KEY_PLACE+ " TEXT, "+
KEY_PLATENUMBER + " TEXT, " +
KEY_SORT + " TEXT, "+
KEY_GRADE+ " TEXT, "+
KEY_DIAMETER + " TEXT, " +
KEY_LENGTH + " TEXT);";
db.execSQL(CREATE_LOGS_TABLE);
}
public ArrayList<Logs> getAllLogsWithSamePlace(String place) {
ArrayList<Logs> logPlaceList = new ArrayList<Logs>();
String selectQuery = "SELECT " + KEY_PLACE + " FROM " + TABLE_LOGS + " WHERE " + KEY_PLACE + " =? " + place;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null); //in this line error
if (cursor.moveToFirst()) {
do {
Logs log = new Logs();
log.setId(cursor.getLong(0));
log.setPlate_number(cursor.getString(1));
log.setSort_id(cursor.getString(2));
log.setGrade(cursor.getString(3));
log.setDiameter(cursor.getString(4));
log.setLength(cursor.getString(5));
}while (cursor.moveToNext());
}
return logPlaceList;
}
这是我的putExtra()
的鳕鱼mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(DisplayLogsByPlace.this, LogsList.class);
intent.putExtra("keyPlace", logsList.get(position).getPlace());
startActivity(intent);
}
});
最后我的getExtras()代码在arrayList行上有错误
String place = (String) getIntent().getExtras().get("keyPlace");
dbHandler = new LogsDBHandler(this);
ArrayList<Logs> logsList = dbHandler.getAllLogsWithSamePlace(place); //error here
这里是有错误的日志
2-22 23:00:52.980 10278-10278/ E/SQLiteLog: (1) near "London": syntax error
02-22 23:00:52.990 10278-10278/ W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4111d540)
02-22 23:00:53.030 10278-10278/ E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{.LogsList}: android.database.sqlite.SQLiteException: near "London": syntax error (code 1): , while compiling: SELECT place FROM Logs WHERE place =? London
我检查所有代码,对我来说似乎没问题。
答案 0 :(得分:2)
如果您将查询值作为参数?
传递,那么您不应该在null
rawQuery()
String selectQuery = "SELECT " + KEY_PLACE + " FROM " + TABLE_LOGS + " WHERE " + KEY_PLACE + "=?" ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, new String[]{place});
答案 1 :(得分:0)
也许是因为“?”符号在何处=?你可以删除它并再次运行吗?