2012-09-07 138 views
0

以下是我的數據庫代碼,我試圖通過更改版本號來升級它,但是當我運行表時不會重新創建或更改...代碼中的任何錯誤plz幫助無法升級數據庫

public class DBHelper extends SQLiteOpenHelper { 
    public static final int db_version = 1; 
    private static final String CL_CITY = null; 
    public static final String COL_NAME = "pName"; 
    //private static final String TABLE_NAME = CL_CITY; 
    public static String TAG = DBHelper.class.getName(); 
    public static String DB_PATH = "data/data/org.clock/database/"; 
    private static String STATUS= "STATUS"; 
    private static String dbName = "clock.db"; 
    private static final String STRING_ALTER = "ALTER TABLE "+CL_CITY+" ADD "+STATUS+" STATUS"; 


    protected SQLiteDatabase dataBase; 
    protected Context context; 
    //private Context context; 
    private static SQLiteDatabase checkDB = null; 
    public DBHelper(Context context, String dbName) { 
     super(context, dbName, null, 34); 
     //this.dbName = dbName; 
     this.context = context; 
    } 

    public void createDb() throws IOException { 
     boolean dbExist = checkDataBase(); 
     Log.d(TAG, "DBChecking............ dbExist : " + dbExist); 
     if (!dbExist) { 
      this.getReadableDatabase(); 
      try { 
       copyDataBase(); 
      } catch (IOException e) { 
       Log.d(TAG, "Error cloneing database" + e); 
      } 
     } 
    } 

    private boolean checkDataBase() { 

     try { 
      if(checkDB == null){ 
       String path = DB_PATH + dbName; 
       Log.d(TAG, "checkDataBase path=" + path); 
       checkDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); 
      } 
     } catch (SQLiteException e) { 
      Log.e(TAG, "DBChecking............ ", e); 
     } 
     /*if (checkDB != null) { 
      checkDB.close(); 
     }*/ 
     return checkDB != null ? true : false; 
    } 

    private void copyDataBase() throws IOException { 
     Log.i(TAG, "copyDataBase database..."); 
     File file = new File(DB_PATH); 
     Log.i(TAG, "cloneDataBase file=" + file); 
     file.mkdirs(); 
     InputStream dbInput = context.getAssets().open(dbName); 
     String outPutFile = DB_PATH + dbName; 
     Log.i(TAG, "copyDataBase outPutFile=" + outPutFile); 
     OutputStream dbOutPut = new FileOutputStream(outPutFile); 
     byte[] buffer = new byte[1024]; 
     Log.i(TAG, "copyDataBase buffer=" + buffer); 
     int length; 
     while ((length = dbInput.read(buffer)) > 0) { 
      dbOutPut.write(buffer, 0, length); 
     } 
     dbOutPut.flush(); 
     dbOutPut.close(); 
     dbInput.close(); 
    } 

    public void openDb() throws SQLException { 
     String dbPath = DB_PATH + dbName; 
     dataBase = SQLiteDatabase.openDatabase(dbPath, null, 
       SQLiteDatabase.OPEN_READWRITE); 
    } 

    @Override 
    public synchronized void close() { 
     if (dataBase != null) 
      dataBase.close(); 
     super.close(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

if (newVersion > oldVersion){ 

    //db.execSQL("CREATE TABLE "+"staff"+" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " 
      // +COL_NAME+" TEXT);"); 

      db.execSQL("STRING_ALTER "); 

    } 
    }} 
+0

你爲什麼不只是調用' copyDataBase()'直接在onUpgrade中。 – MKJParekh

+0

可以告訴我在完善的解決方案中,onupgrade方法 – hari86

回答

4

變化db.execSQL("STRING_ALTER ");db.execSQL(STRING_ALTER);

還要檢查你的alter SQL命令。它看起來很不對。您使用了錯誤的數據庫路徑

+0

+1的代碼是什麼。 –

0

我聲明:

public static String DB_PATH = "data/data/org.clock/database/"; 

這應該是:

public static String DB_PATH = "data/data/org.clock/databases/"; 

(數據庫/ ==>數據庫/)

+0

也檢查此博客文章,以獲得使用預加載的數據庫與「SQLiteOpenHelper」更清潔的解決方案。 http://blog.kdehairy.com/2012/08/19/using-a-preloaded-sqlite-database-with-sqliteopenhelper/ – kdehairy

+0

我有兩個數據庫文件夾,當我檢查DDMS文件資源管理器一個與數據庫和另一個數據庫(s).... – hari86

+0

@ kdehairy-關於博客中的代碼如果我們想要升級數據庫,我們需要在資產中創建一個新的數據庫文件夾?或者我們可以通過更改版本名稱在onUpgrade()方法中,在onupgrade()方法中提供alterquires()方法 – hari86