2012-09-07 132 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