2014-04-15 4 views
1

데이터베이스로 sqlite를 사용하여 android app의 요소를 업데이트하려고합니다. 하지만 SQLite 예외 오류가 발생합니다 : 해당 열이 없습니다.Sqlite 업데이트 오류가 없습니다.

내 로그 캣은 다음과 같습니다

04-15 18:46:19.277 5290-5290/com.example.testlayout.app E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.testlayout.app, PID: 5290 
    android.database.sqlite.SQLiteException: no such column: id (code 1): , while compiling: UPDATE pontos SET Estado=?,Armadura=?,Observações=?,Serial=?,NrColuna=?,Rede=?,Lampada=?,tir=?,Y=?,Potencia=?,X=?,Coluna=? WHERE id=? 
      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
      at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1572) 
      at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1520) 
      at com.example.testlayout.app.PointsDBAdapter.updatePoint(PointsDBAdapter.java:85) 
      at com.example.testlayout.app.CreatePoint$1.onClick(CreatePoint.java:75) 
      at android.view.View.performClick(View.java:4438) 
      at android.view.View$PerformClick.run(View.java:18422) 
      at android.os.Handler.handleCallback(Handler.java:733) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5017) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
      at dalvik.system.NativeStart.main(Native Method) 

또한에 업데이트 클래스가 내 publicAdapter 공유됩니다

public class PointsDBAdapter { 
    private SQLiteDatabase database; 
    private PointsDB points; 
    private String[] allColumns = {PointsDB.ID, PointsDB.SERIAL, PointsDB.OBSERVATIONS, PointsDB.TIR, PointsDB.X, PointsDB.Y, 
    PointsDB.NETWORKNUMBER, PointsDB.POTENCIA, PointsDB.COLUMNNR, PointsDB.COLUNA, PointsDB.LAMP, PointsDB.ARMOR, PointsDB.LAMPSTATE}; 

    public PointsDBAdapter(Context context) { 
      points = new PointsDB(context); 
      open(); 
    } 

    public void open() throws SQLException { 
     database = points.getWritableDatabase(); 
    } 

    public Point createPoint(String Serial, String Observation, boolean tir, double x, double y, int networkNumber, int intensity, 
           int ColumnNr, int Column, int lamp, int armor, int lampState) { 
     ContentValues values = new ContentValues(); 
     values.put(PointsDB.SERIAL, Serial); 
     values.put(PointsDB.OBSERVATIONS, Observation); 
     values.put(PointsDB.TIR, tir); 
     values.put(PointsDB.X, x); 
     values.put(PointsDB.Y, y); 
     values.put(PointsDB.NETWORKNUMBER, networkNumber); 
     values.put(PointsDB.POTENCIA, intensity); 
     values.put(PointsDB.COLUMNNR, ColumnNr); 
     values.put(PointsDB.COLUNA, Column); 
     values.put(PointsDB.LAMP, lamp); 
     values.put(PointsDB.ARMOR, armor); 
     values.put(PointsDB.LAMPSTATE, lampState); 
     long insertId = database.insert(PointsDB.TABLE_NAME, null, values); 
     // To show how to query 

     Cursor cursor = database.query(PointsDB.TABLE_NAME, allColumns, PointsDB.ID + " = " + 
       insertId, null,null, null, null); 
     cursor.moveToFirst(); 
     return cursorToPoint(cursor); 
    } 

    private Point cursorToPoint(Cursor cursor) { 
     Point point = new 
       Point(cursor.getString(1), cursor.getString(2), Boolean.parseBoolean(cursor.getString(3)), Double.parseDouble(cursor.getString(4)), 
       Double.parseDouble(cursor.getString(5)), Integer.parseInt(cursor.getString(6)), Integer.parseInt(cursor.getString(7)), 
       Integer.parseInt(cursor.getString(8)), Integer.parseInt(cursor.getString(9)), Integer.parseInt(cursor.getString(10)), 
       Integer.parseInt(cursor.getString(11)), Integer.parseInt(cursor.getString(12))); 
     return point; 
    } 

    public void updatePoint(int idPoint, String Serial, String Observation, boolean tir, double x, double y, int networkNumber, int intensity, 
          int ColumnNr, int Column, int lamp, int armor, int lampState){ 
     String where = "id=?"; 
     String[] whereArgs = new String[] {String.valueOf(idPoint)}; 
     ContentValues values = new ContentValues(); 
     values.put(PointsDB.SERIAL, Serial); 
     values.put(PointsDB.OBSERVATIONS, Observation); 
     values.put(PointsDB.TIR, tir); 
     values.put(PointsDB.X, x); 
     values.put(PointsDB.Y, y); 
     values.put(PointsDB.NETWORKNUMBER, networkNumber); 
     values.put(PointsDB.POTENCIA, intensity); 
     values.put(PointsDB.COLUMNNR, ColumnNr); 
     values.put(PointsDB.COLUNA, Column); 
     values.put(PointsDB.LAMP, lamp); 
     values.put(PointsDB.ARMOR, armor); 
     values.put(PointsDB.LAMPSTATE, lampState); 
     database.update(PointsDB.TABLE_NAME, values, where, whereArgs); 
    } 

    public Point getPoint(int idPoint){ 
     Log.d("tag", "estou a entrar com um valor de idPoint = " + idPoint); 
     Cursor cursor = database.query(PointsDB.TABLE_NAME, allColumns, PointsDB.ID + " = " + 
       idPoint, null,null, null, null); 
     if(cursor.moveToFirst()){ 
      return cursorToPoint(cursor); 
     } else 
      return null; 
    } 

    public long count() { 
     return DatabaseUtils.queryNumEntries(database,PointsDB.TABLE_NAME); 
    } 
} 

이 내 DBhelper 클래스입니다 :

public class PointsDB extends SQLiteOpenHelper { 

    public static final String ID = "_id"; 
    public static final String SERIAL = "Serial"; 
    public static final String OBSERVATIONS = "Observações"; 
    public static final String TIR = "tir"; 
    public static final String X = "X"; 
    public static final String Y = "Y"; 
    public static final String NETWORKNUMBER = "Rede"; 
    public static final String POTENCIA = "Potencia"; 
    public static final String COLUMNNR = "NrColuna"; 
    public static final String COLUNA = "Coluna"; 
    public static final String LAMP = "Lampada"; 
    public static final String ARMOR = "Armadura"; 
    public static final String LAMPSTATE = "Estado"; 
    private static final int DATABASE_VERSION = 3; 

    private static final String DATABASE_NAME = "pontos.db"; 
    public static final String TABLE_NAME = "pontos"; 
    private static final String DATABASE_CREATE = "create table " 
      + TABLE_NAME + "(" + ID 
      + " integer primary key autoincrement, " + SERIAL 
      + " text not null, " + OBSERVATIONS + " text not null, " 
      + TIR + " text not null" + 
      ", " + X + " text not null, " + Y + " text not null, " + NETWORKNUMBER + " text not null, " + POTENCIA + " text not null, " 
      + COLUMNNR + " text not null, " + COLUNA + " text not null, " + LAMP + " text not null, " 
      + ARMOR + " text not null, " + LAMPSTATE + " text not null);"; 

    public PointsDB(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(PointsDB.class.getName(), 
       "Upgrading database from version " + oldVersion + " to " 
         + newVersion + ", which will destroy all old data" 
     ); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db); 
    } 
} 

그리고 마지막으로 이것은 내가 메인 클래스에서 메서드를 호출하는 곳입니다 :

그것은 당신의 열 이름은 _id하지 ID입니다 나에게 알 16,
public void onClick(View view) { 
        String Serial = SerialField.getText().toString(); 
        String Observation = ObservationField.getText().toString(); 
        double X = Double.parseDouble(XField.getText().toString()); 
        double Y = Double.parseDouble(YField.getText().toString()); 
        int columnNumber = Integer.parseInt(columnNrField.getText().toString()); 
        int intensity = Integer.parseInt(intensityField.getText().toString()); 
        int networkToSet = networkSpinner.getSelectedItemPosition(); 
        boolean checkBoxStatus = status.isChecked(); 
        int valToSet = spinner.getSelectedItemPosition(); 
        int setColumn = columnSpinner.getSelectedItemPosition(); 
        int setArmor = armor.getSelectedItemPosition(); 
        int setLamp = lamp.getSelectedItemPosition(); 

         pointsList.updatePoint(pos, Serial, Observation, checkBoxStatus, X, Y, networkToSet, columnNumber, intensity, setColumn, setArmor, setLamp, valToSet); 
+0

앱을 제거한 후 다시 설치하거나 데이터베이스 버전을 변경하고 다시 실행 해보세요. – Raghunandan

답변

2

.]

여기에서 확인할 수있다.

public static final String ID = "_id"; 

여기 그러나

private static final String DATABASE_CREATE = "create table " 
      + TABLE_NAME + "(" + ID 
      + " integer primary key autoincrement, " + SERIAL 
      + " text not null, " + OBSERVATIONS + " text not null, " 
      + TIR + " text not null" + 
      ", " + X + " text not null, " + Y + " text not null, " + NETWORKNUMBER + " text not null, " + POTENCIA + " text not null, " 
      + COLUMNNR + " text not null, " + COLUNA + " text not null, " + LAMP + " text not null, " 
      + ARMOR + " text not null, " + LAMPSTATE + " text not null);"; 

, 당신은 "= ID?"에서하는 ID 열이 아닌

public void updatePoint(int idPoint, String Serial, String Observation, boolean tir, double x, double y, int networkNumber, int intensity, 
          int ColumnNr, int Column, int lamp, int armor, int lampState){ 
     String where = "id=?"; 

가 그냥 어디를 변경 _id 컬럼을 찾고 있습니다 ~ "_id =?"