【发布时间】:2021-06-07 11:42:39
【问题描述】:
我正在尝试使用 SQLite 数据库,它是一个已经填充的数据库,经过一些研究我发现我需要复制这个数据库才能使用它,所以我拿起代码并测试了它,但副本不完整,所有列都没有被复制,数据只是不存在。当我用 DB Browser for SQLite 打开原始文件时没有问题。
这里是框架databasehelper的代码:
package com.example.chiffrage;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.TextView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class DataBaseHelper extends SQLiteOpenHelper {
String DB_PATH = null;
private static String DB_NAME = "ChiffrageBDD.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 10);
this.myContext = context;
this.DB_PATH = context.getApplicationInfo().dataDir + "/";
Log.e("Path 1", DB_PATH);
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} 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) {
// }
// if (checkDB != null) {
// checkDB.close();
// }
// return checkDB != null ? true : false;
File databasePath = myContext.getDatabasePath(DB_NAME);
return databasePath.exists();
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[2068];
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();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion)
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
public List getAllMetal(){
List returnList = new ArrayList();
String queryString = "PRAGMA table_info(Metal)";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(queryString, null);
if (cursor.moveToFirst()){
do {
returnList.add(cursor.getString(1));
} while (cursor.moveToNext());
} else {
//failure
}
cursor.close();
db.close();
return returnList;
}
}
getAllMetal 的响应是“[ID]” 我应该有一个带有“id”和“nom”的表名“Metal” nom 是金属的名称。 没有数据,我使用 PRAGMA 来验证该列,如您所见,缺少“nom”列。
这里有一些我得到代码的资源:
[复制数据库] Getting "Failed to open database" error when copying a sqlite database from assets In Android 4.2
你的时间。
【问题讨论】:
-
当您尝试复制填充的数据库时,它位于何处?你想把它复制到哪里?
标签: java android sqlite android-studio sqliteopenhelper