2016-02-22 97 views
0

我收到語法錯誤代碼1,無法解決它..當我嘗試從數據庫中獲取數據我得到這個錯誤。我看起來一切似乎都沒問題。我使用putExtra()和getExtras()來獲取變量並按它排序。 這裏是我的錯誤代碼。 這是我的Dbhandler,在遊標行上有錯誤。SQLiteLog語法錯誤代碼1 - android

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(COD)

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); 
      } 
     }); 

終於在ArrayList的線我的getExtras(),錯誤代碼

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 

我檢查所有代碼對我來說似乎沒問題..

回答

2

如果您傳遞查詢值作爲參數?,那麼你是不是想在第二個參數來傳遞nullrawQuery()

String selectQuery = "SELECT " + KEY_PLACE + " FROM " + TABLE_LOGS + " WHERE " + KEY_PLACE + "=?" ; 
SQLiteDatabase db = this.getWritableDatabase(); 
Cursor cursor = db.rawQuery(selectQuery, new String[]{place}); 

訪問rawQuery (String sql, String[] selectionArgs)

0

也許是由於「?」符號在哪裏地方= ?.你能刪除它並再次運行?

相關問題