Reputation: 835
I'm getting syntax error code 1 and cannot resolve it..when I try to get data from db i get this error. I look everything an to it seems alright. I'm using putExtra() and getExtras() to get variable and sort by it. here is my code where errors are. This is my Dbhandler with error on cursor line.
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;
}
Here is my cod for 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);
}
});
and finally my code for getExtras() with error on arrayList line
String place = (String) getIntent().getExtras().get("keyPlace");
dbHandler = new LogsDBHandler(this);
ArrayList<Logs> logsList = dbHandler.getAllLogsWithSamePlace(place); //error here
and here are logs with errors
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
I check all code and seems alright to me..
Upvotes: 0
Views: 248
Reputation: 9009
If you are passing query values as parameters ?
, then you are not suppose to pass null
at second parameter of rawQuery()
String selectQuery = "SELECT " + KEY_PLACE + " FROM " + TABLE_LOGS + " WHERE " + KEY_PLACE + "=?" ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, new String[]{place});
visit rawQuery (String sql, String[] selectionArgs)
Upvotes: 2
Reputation: 1794
Maybe it due to the "?" symbol at WHERE place =?. Could you remove it and run again?
Upvotes: 0