我知道这个问题以前被问过很多次,但我不知道。
我在第一次启动时使用说明here的方法从资产文件夹复制数据库。但是当我启动该应用程序时,它崩溃并显示以下错误:
android.database.sqlite.SQLiteException: table "TOOL" already exists (code 1): , while compiling: CREATE TABLE "TOOL" ("_id" INTEGER PRIMARY KEY ,"NAME" TEXT NOT NULL ,"IMAGE" TEXT,"TYPE" INTEGER NOT NULL ,"LAST_USED_DATE" INTEGER NOT NULL );
我使用GreenDao生成的SQlite脚本自己创建了表,因此我的数据库与GreenDao尝试创建的数据库相同。
我为强迫GreenDao接受我自己的数据库做了什么?
这是我的DatabaseHelper类,以防您需要阅读它:
public class DatabaseHelper extends DaoMaster.OpenHelper {
private SQLiteDatabase db;
private DaoMaster daoMaster;
private Context context;
private static DatabaseHelper mInstance;
private DaoSession daoSession;
private SQLiteDatabase sqliteDatabase;
private static String DB_PATH;
private static String DB_NAME = "main-db";
public DatabaseHelper(Context context, SQLiteDatabase.CursorFactory factory) {
super(context, DB_NAME, factory);
this.context = context;
if (android.os.Build.VERSION.SDK_INT >= 17) {
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
} else {
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
try {
createDataBase();
} catch (Exception ioe) {
throw new Error("Unable to create database");
}
}
public static DatabaseHelper getInstance(Context context) {
if (mInstance != null)
return mInstance;
mInstance = new DatabaseHelper(context.getApplicationContext(), null);
mInstance.db = mInstance.getWritableDatabase();
mInstance.daoMaster = new DaoMaster(mInstance.db);
mInstance.daoSession = mInstance.daoMaster.newSession();
return mInstance;
}
public DaoMaster getDaoMaster() {
return daoMaster;
}
public DaoSession getDaoSession() {
return daoSession;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
/**
* Open Database for Use
*/
public void openDatabase() {
String databasePath = DB_PATH + DB_NAME;
sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
(SQLiteDatabase.OPEN_READWRITE));
}
/**
* Close Database after use
*/
@Override
public synchronized void close() {
if ((sqliteDatabase != null) && sqliteDatabase.isOpen()) {
sqliteDatabase.close();
}
super.close();
}
/**
* Get database instance for use
*/
public SQLiteDatabase getSqliteDatabase() {
return sqliteDatabase;
}
/**
* Create new database if not present
*/
public void createDataBase() {
SQLiteDatabase sqliteDatabase = null;
if (databaseExists()) {
/* Check for Upgrade */
} else {
/* Database does not exists create blank database */
sqliteDatabase = this.getReadableDatabase();
sqliteDatabase.close();
copyDataBase();
}
}
/**
* Check Database if it exists
*/
private boolean databaseExists() {
SQLiteDatabase sqliteDatabase = null;
try {
String databasePath = DB_PATH + DB_NAME;
sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (sqliteDatabase != null) {
sqliteDatabase.close();
}
return sqliteDatabase != null ? true : false;
}
/**
* Copy existing database file in system
*/
public void copyDataBase() {
int length;
byte[] buffer = new byte[1024];
String databasePath = DB_PATH + DB_NAME;
try {
InputStream databaseInputFile = this.context.getAssets().open("databases/" + DB_NAME + ".sqlite");
OutputStream databaseOutputFile = new FileOutputStream(databasePath);
while ((length = databaseInputFile.read(buffer)) > 0) {
databaseOutputFile.write(buffer, 0, length);
databaseOutputFile.flush();
}
databaseInputFile.close();
databaseOutputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}