2013-08-22 85 views
0

目前我有這樣升級sqllite數據庫架構

public class AABDatabaseManager 
{ 
    // the Activity or Application that is creating an object from this class. 
    Context context; 

    // a reference to the database used by this application/object 
    private SQLiteDatabase db; 

    // These constants are specific to the database. They should be 
    // changed to suit your needs. 
    private final String DB_NAME = "database_name"; 
    private final int DB_VERSION = 1; 

    // These constants are specific to the database table. They should be 
    // changed to suit your needs. 
    private final String TABLE_NAME = "database_table"; 
    private final String TABLE_ROW_ID = "id"; 
    private final String TABLE_ROW_ONE = "table_row_one"; 
    private final String TABLE_ROW_TWO = "table_row_two"; 

    public AABDatabaseManager(Context context) 
    { 
     this.context = context; 

     // create or open the database 
     CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context); 
     this.db = helper.getWritableDatabase(); 
    } 


    public void addRow(String rowStringOne, String rowStringTwo) 
    { 
     // this is a key value pair holder used by android's SQLite functions 
     ContentValues values = new ContentValues(); 
     values.put(TABLE_ROW_ONE, rowStringOne); 
     values.put(TABLE_ROW_TWO, rowStringTwo); 

     // ask the database object to insert the new data 
     try{db.insert(TABLE_NAME, null, values);} 
     catch(Exception e) 
     { 
      Log.e("DB ERROR", e.toString()); 
      e.printStackTrace(); 
     } 
    } 


    public void deleteRow(long rowID) 
    { 
     // ask the database manager to delete the row of given id 
     try {db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);} 
     catch (Exception e) 
     { 
      Log.e("DB ERROR", e.toString()); 
      e.printStackTrace(); 
     } 
    } 


    public void updateRow(long rowID, String rowStringOne, String rowStringTwo) 
    { 
     // this is a key value pair holder used by android's SQLite functions 
     ContentValues values = new ContentValues(); 
     values.put(TABLE_ROW_ONE, rowStringOne); 
     values.put(TABLE_ROW_TWO, rowStringTwo); 

     // ask the database object to update the database row of given rowID 
     try {db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);} 
     catch (Exception e) 
     { 
      Log.e("DB Error", e.toString()); 
      e.printStackTrace(); 
     } 
    } 


    public ArrayList<Object> getRowAsArray(long rowID) 
    { 
     // create an array list to store data from the database row. 
     // I would recommend creating a JavaBean compliant object 
     // to store this data instead. That way you can ensure 
     // data types are correct. 
     ArrayList<Object> rowArray = new ArrayList<Object>(); 
     Cursor cursor; 

     try 
     { 
      // this is a database call that creates a "cursor" object. 
      // the cursor object store the information collected from the 
      // database and is used to iterate through the data. 
      cursor = db.query 
      (
        TABLE_NAME, 
        new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO }, 
        TABLE_ROW_ID + "=" + rowID, 
        null, null, null, null, null 
      ); 

      // move the pointer to position zero in the cursor. 
      cursor.moveToFirst(); 

      // if there is data available after the cursor's pointer, add 
      // it to the ArrayList that will be returned by the method. 
      if (!cursor.isAfterLast()) 
      { 
       do 
       { 
        rowArray.add(cursor.getLong(0)); 
        rowArray.add(cursor.getString(1)); 
        rowArray.add(cursor.getString(2)); 
       } 
       while (cursor.moveToNext()); 
      } 

      // let java know that you are through with the cursor. 
      cursor.close(); 
     } 
     catch (SQLException e) 
     { 
      Log.e("DB ERROR", e.toString()); 
      e.printStackTrace(); 
     } 

     // return the ArrayList containing the given row from the database. 
     return rowArray; 
    } 



    public ArrayList<ArrayList<Object>> getAllRowsAsArrays() 
    { 
     // create an ArrayList that will hold all of the data collected from 
     // the database. 
     ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>(); 

     // this is a database call that creates a "cursor" object. 
     // the cursor object store the information collected from the 
     // database and is used to iterate through the data. 
     Cursor cursor; 

     try 
     { 
      // ask the database object to create the cursor. 
      cursor = db.query(
        TABLE_NAME, 
        new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO}, 
        null, null, null, null, null 
      ); 

      // move the cursor's pointer to position zero. 
      cursor.moveToFirst(); 

      // if there is data after the current cursor position, add it 
      // to the ArrayList. 
      if (!cursor.isAfterLast()) 
      { 
       do 
       { 
        ArrayList<Object> dataList = new ArrayList<Object>(); 

        dataList.add(cursor.getLong(0)); 
        dataList.add(cursor.getString(1)); 
        dataList.add(cursor.getString(2)); 

        dataArrays.add(dataList); 
       } 
       // move the cursor's pointer up one position. 
       while (cursor.moveToNext()); 
      } 
     } 
     catch (SQLException e) 
     { 
      Log.e("DB Error", e.toString()); 
      e.printStackTrace(); 
     } 

     // return the ArrayList that holds the data collected from 
     // the database. 
     return dataArrays; 
    } 

    private class CustomSQLiteOpenHelper extends SQLiteOpenHelper 
    { 
     public CustomSQLiteOpenHelper(Context context) 
     { 
      super(context, DB_NAME, null, DB_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      // This string is used to create the database. It should 
      // be changed to suit your needs. 
      String newTableQueryString = "create table " + 
             TABLE_NAME + 
             " (" + 
             TABLE_ROW_ID + " integer primary key autoincrement not null," + 
             TABLE_ROW_ONE + " text," + 
             TABLE_ROW_TWO + " text" + 
             ");"; 
      // execute the query string to the database. 
      db.execSQL(newTableQueryString); 
     } 
     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
     { 
      // NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION. 
      // OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE. 
     } 
    } 
} 

我的數據庫代碼現在我需要其他3列添加到我的桌子從而更新我的數據庫架構時,我用Google搜索,我發現我需要調用與sqllitehelper構造新的版本號,所以我改變了我的代碼如下 但仍然它不是調用onupgrade()

public class AABDatabaseManager 
{ 
    // the Activity or Application that is creating an object from this class. 
    Context context; 

    // a reference to the database used by this application/object 
    private SQLiteDatabase db; 

    // These constants are specific to the database. They should be 
    // changed to suit your needs. 
    static final String DB_NAME = "database_name"; 
    static final int DB_VERSION = 1; 

    // These constants are specific to the database table. They should be 
    // changed to suit your needs. 
    private final String TABLE_NAME = "database_table"; 
    private final String TABLE_ROW_ID = "id"; 
    private final String TABLE_ROW_ONE = "table_row_one"; 
    private final String TABLE_ROW_TWO = "table_row_two"; 
    private final String TABLE_ROW_THREE = "table_row_three"; 
    private final String TABLE_ROW_FOUR = "table_row_four"; 
    private final String TABLE_ROW_FIVE= "table_row_five"; 
    public AABDatabaseManager(Context context) 
    { 
     //super(context, DB_NAME, null,2); 
     this.context = context; 

     // create or open the database 
     CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context); 
     this.db = helper.getWritableDatabase(); 
    } 

    public void addRow(String rowStringOne, String rowStringTwo, String rowStringThree, String rowStringFour, String rowStringFive) 
    { 
     // this is a key value pair holder used by android's SQLite functions 
     ContentValues values = new ContentValues(); 
     values.put(TABLE_ROW_ONE, rowStringOne); 
     values.put(TABLE_ROW_TWO, rowStringTwo); 
     values.put(TABLE_ROW_THREE, rowStringThree); 
     values.put(TABLE_ROW_FOUR, rowStringFour); 
     values.put(TABLE_ROW_FIVE, rowStringFive); 
     // ask the database object to insert the new data 
     try{db.insert(TABLE_NAME, null, values);} 
     catch(Exception e) 
     { 
      Log.e("DB ERROR", e.toString()); 
      e.printStackTrace(); 
     } 
    } 


    public void deleteRow(long rowID) 
    { 
     // ask the database manager to delete the row of given id 
     try {db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);} 
     catch (Exception e) 
     { 
      Log.e("DB ERROR", e.toString()); 
      e.printStackTrace(); 
     } 
    } 

    public void updateRow(long rowID, String rowStringOne, String rowStringTwo,String rowStringThree, String rowStringFour,String rowStringFive) 
    { 
     // this is a key value pair holder used by android's SQLite functions 
     ContentValues values = new ContentValues(); 
     values.put(TABLE_ROW_ONE, rowStringOne); 
     values.put(TABLE_ROW_TWO, rowStringTwo); 
     values.put(TABLE_ROW_THREE, rowStringThree); 
     values.put(TABLE_ROW_FOUR, rowStringFour); 
     values.put(TABLE_ROW_FIVE, rowStringFive); 


     // ask the database object to update the database row of given rowID 
     try {db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);} 
     catch (Exception e) 
     { 
      Log.e("DB Error", e.toString()); 
      e.printStackTrace(); 
     } 
    } 

    public ArrayList<Object> getRowAsArray(long rowID) 
    { 
     // create an array list to store data from the database row. 
     // I would recommend creating a JavaBean compliant object 
     // to store this data instead. That way you can ensure 
     // data types are correct. 
     ArrayList<Object> rowArray = new ArrayList<Object>(); 
     Cursor cursor; 

     try 
     { 
      // this is a database call that creates a "cursor" object. 
      // the cursor object store the information collected from the 
      // database and is used to iterate through the data. 
      cursor = db.query 
      (
        TABLE_NAME, 
        new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO, TABLE_ROW_THREE, TABLE_ROW_FOUR, TABLE_ROW_FIVE }, 
        TABLE_ROW_ID + "=" + rowID, 
        null, null, null, null, null 
      ); 

      // move the pointer to position zero in the cursor. 
      cursor.moveToFirst(); 

      // if there is data available after the cursor's pointer, add 
      // it to the ArrayList that will be returned by the method. 
      if (!cursor.isAfterLast()) 
      { 
       do 
       { 
        rowArray.add(cursor.getLong(0)); 
        rowArray.add(cursor.getString(1)); 
        rowArray.add(cursor.getString(2)); 
        rowArray.add(cursor.getString(3)); 
        rowArray.add(cursor.getString(4)); 
        rowArray.add(cursor.getString(5)); 
       } 
       while (cursor.moveToNext()); 
      } 

      // let java know that you are through with the cursor. 
      cursor.close(); 
     } 
     catch (SQLException e) 
     { 
      Log.e("DB ERROR", e.toString()); 
      e.printStackTrace(); 
     } 

     // return the ArrayList containing the given row from the database. 
     return rowArray; 
    } 


    public ArrayList<ArrayList<Object>> getAllRowsAsArrays() 
    { 
     // create an ArrayList that will hold all of the data collected from 
     // the database. 
     ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>(); 

     // this is a database call that creates a "cursor" object. 
     // the cursor object store the information collected from the 
     // database and is used to iterate through the data. 
     Cursor cursor; 

     try 
     { 
      // ask the database object to create the cursor. 
      cursor = db.query(
        TABLE_NAME, 
        new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO, TABLE_ROW_THREE, TABLE_ROW_FOUR, TABLE_ROW_FIVE}, 
        null, null, null, null, null 
      ); 

      // move the cursor's pointer to position zero. 
      cursor.moveToFirst(); 

      // if there is data after the current cursor position, add it 
      // to the ArrayList. 
      if (!cursor.isAfterLast()) 
      { 
       do 
       { 
        ArrayList<Object> dataList = new ArrayList<Object>(); 

        dataList.add(cursor.getLong(0)); 
        dataList.add(cursor.getString(1)); 
        dataList.add(cursor.getString(2)); 
        dataList.add(cursor.getString(3)); 
        dataList.add(cursor.getString(4)); 
        dataList.add(cursor.getString(5)); 
        dataArrays.add(dataList); 
       } 
       // move the cursor's pointer up one position. 
       while (cursor.moveToNext()); 
      } 
     } 
     catch (SQLException e) 
     { 
      Log.e("DB Error", e.toString()); 
      e.printStackTrace(); 
     } 

     // return the ArrayList that holds the data collected from 
     // the database. 
     return dataArrays; 
    } 

    private class CustomSQLiteOpenHelper extends SQLiteOpenHelper 
    { 
     public CustomSQLiteOpenHelper(Context context) 
     { 
      super(context, DB_NAME,null, 2); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      // This string is used to create the database. It should 
      // be changed to suit your needs. 
      String newTableQueryString = "create table " + 
             TABLE_NAME + 
             " (" + 
             TABLE_ROW_ID + " integer primary key autoincrement not null," + 
             TABLE_ROW_ONE + " text," + 
             TABLE_ROW_TWO + " text," + 
             TABLE_ROW_THREE + " text," + 
             TABLE_ROW_FOUR + " text," + 
             TABLE_ROW_FIVE+ " text" + 
             ");"; 
      // execute the query string to the database. 
      db.execSQL(newTableQueryString); 
     } 


     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
     { 
      // NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION. 
      // OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE. 
      db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); 
      onCreate(db); 
     } 
    } 

} 

請原諒長期的問題,我認爲這將有助於PPL回答。所以告訴我可能是什麼問題,爲什麼我無法更改我的數據庫結構。

現在,當我運行代碼我得到錯誤,如未知列table_row_three

和我從其他服務調用此類似這樣的

offlinedb= new AABDatabaseManager(this); 

    offlinedb.addRow(longitude, latitude,imei,"check",date); 
+0

你新的行後缺少逗號:看到你的onCreate方法 –

+0

@ M.Bennett我編輯,現在得到無法創建數據庫時,我檢查我的日誌 –

+0

在你的SQLLiteOpenHelper超類的調用中,我會堅持使用DB_VERSION常量 –

回答

0

編輯:忽視了你的實例調用,你definetely失蹤getWritableDatabase()在寫入數據庫之前,並且在處理它時,在使用遊標時將TABLE_ROW_ID contstant值更改爲「_id」......應該立即工作。

String newTableQueryString = "create table if not exists " + 
            TABLE_NAME + " (" + 
            TABLE_ROW_ID + " integer primary key autoincrement not null, " + 
            TABLE_ROW_ONE + " text, " + 
            TABLE_ROW_TWO + " text, " + 
            TABLE_ROW_THREE + " text, " + 
            TABLE_ROW_FOUR + " text, " + 
            TABLE_ROW_FIVE + " text);"; 

這就是我怎麼會做,因爲SQLite是發或停發,我們至少知道你的onCreate()是

+0

9月8日至22日:32:26.862:E /錯誤(11948):不能創建數據庫正在此錯誤 –

+0

嘗試 \t \t { \t \t offlinedb =新AABDatabaseManager(此); \t \t} \t \t趕上(例外五) \t \t { \t \t \t Log.e( 「錯誤」, 「無法創建數據庫」); \t \t \t e.printStackTrace(); \t \t} –

+0

得到這個錯誤是什麼問題? –

0

或者,如果你有足夠多的時間來使用一個新的框架,來看看這裏:

http://adaframework.com/

我在一個會議上聽說過,它看上去非常酷。

它管理透明的升級...

我一直說,我還沒有測試過,但它是非常觀賞壯麗看到在會議上做一次管理一個數據庫中的Android與那傢伙框架。

+0

是的,肯定是我看,謝謝你的建議 –

0

當你發現你在代碼中沒有錯誤,並且所有的東西都能正常工作,但是你的數據庫仍然沒有升級,增加你的版本號可能會對你有幫助。那就是我所做的。希望它可以幫助其他人

0

如果沒有任何重要數據存儲在數據庫中,我建議您刪除數據庫並重新設置它。因爲數據庫結構在設置後不會接受任何更改。因此,即使您解決了一個問題(例如,導致列未找到錯誤的缺少逗號,問題也無法解決)。因爲您更改了數據庫的版本,所以它工作正常。

在數據庫結構(而不​​是方法)中進行更改後,重置您的數據庫或版本。

(組去DDMS查看 - >文件瀏覽器 - >數據 - >數據 - >你的包名 - >數據庫)