android.database.sqlite.SQLiteException:没有这样的表:cures(代码1):

时间:2017-03-21 13:37:28

标签: android database sqlite android-sqlite

  1. 我更改了数据库用户版本
  2. 在我创建的课程中,我更改了超级(...)
  3. 中的版本号
  4. 在onupgrade方法中,我删除了数据库并通过应用程序中的副本创建了一个新数据库。
  5. 我已经在stackoverflow上检查了这个问题的所有答案。每次编译似乎都失败了。
  6. 我试过网络寻找一个似乎逃避我的解决方案。

    我将不胜感激。

    课程如下:

    public class DBAdapter extends SQLiteOpenHelper {
        // DBPATH uses the default system path for a given application
        // /data/data/<app namespace> , which in our example will be
        // com.wordpress.arogyavidya.mooligaimarmam
        private static String DBPATH = "/data/data/com.wordpress.arogyavidya.mooligaimarmam/databases/";
        private static String DBNAME = "mooligaimarmam.db";
        //  private static String DATABASE_TABLE = "lkgchef";
    //  static final String KEY_ROWID = "_id";
    //  static final String KEY_NAME = "name";
    //  static final String KEY_DETAIL = "desc";
        private SQLiteDatabase myDatabase;
        private final Context myContext;
    
        // constructor
        public DBAdapter(Context context) {
            super(context, DBNAME, null, 3);
            this.myContext = context;
            //DBPATH = this.myContext.getDatabasePath("mooligaimarmam.db").getPath();
        }
    
        // create an empty db, and replace with our chosen db
        public void createDatabase() throws IOException {
            if (!checkDatabase()) {
                this.getWritableDatabase();
                try {
                    copyDatabase();
                } catch (IOException e) {
                    throw new Error("Error copying database from system assets");
                }
            }
        }
    
        // Check if our database already exists
        private boolean checkDatabase() {
            SQLiteDatabase checkableDatabase = null;
            try {
                checkableDatabase = SQLiteDatabase.openDatabase(DBPATH+DBNAME,
                        null, SQLiteDatabase.OPEN_READONLY);
            } catch (SQLiteException e) {
    
                // our database doesn't exist, so we'll return false below.
            }
            if (checkableDatabase != null) {
                checkableDatabase.close();
            }
            return checkableDatabase != null ? true : false;
        }
    
        // Copy our database from the Application's assets over the empty DB for use
        private void copyDatabase() throws IOException {
            InputStream myInput = myContext.getAssets().open(DBNAME);
            OutputStream myOutput = new FileOutputStream(DBPATH+DBNAME);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }
    
        public void openDatabase() throws SQLException {
            myDatabase = SQLiteDatabase.openDatabase(DBPATH + DBNAME, null,
                    SQLiteDatabase.OPEN_READWRITE);
            /*myDatabase = SQLiteDatabase.openDatabase(DBPATH, null,
                    SQLiteDatabase.OPEN_READWRITE);*/
        }
    
        @Override
        public synchronized void close() {
            if (myDatabase != null)
                myDatabase.close();
            super.close();
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            // Handle creation tasks, etc.
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // Handle upgrade tasks, etc.
            myDatabase.close();
            myContext.deleteDatabase(DBPATH+DBNAME);
    
            try {
                this.createDatabase();
            } catch (IOException e) {
                throw new Error("Error copying database from system assets");
            }
        }
    }
    

    感谢。

1 个答案:

答案 0 :(得分:0)

尝试一次!不要删除数据库,只需删除表。这对我来说每次都适用。

 @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_Name);
    onCreate(db);
}