【发布时间】:2018-01-04 10:29:02
【问题描述】:
我有一个带有我的数据库的 mySQL 脚本,我想使用该脚本将此数据库导入我的 android 应用程序,当我执行此代码时它可以工作(我没有任何异常或错误)但是当我尝试从 DB 获取信息它不起作用。
private static String DB_PATH = "/data/data/com.example.importDB/databases/";
private static String DB_NAME = "mydb.sql";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getResources().openRawResource(R.raw.mydb);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
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 {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
我的“mydb.sql”在 res/raw 文件夹中
这是 mySql 脚本:
https://mega.nz/#!KUkA0JRY!r2BsQwmJbtmxgTAB1DZVpG-j_JO4oEhY9oIfg3K4Ohk
谢谢
【问题讨论】:
-
您的脚本链接要求提供“解密密钥” - 另外,您是否正在导入脚本?或者尝试运行脚本来导入数据?一个 mySQL DB 应该有多个文件...
-
我更新了链接,我正在尝试运行脚本并生成包含所有信息的数据库
标签: android mysql sql-scripts