gpt4 book ai didi

android - 无法打开数据库 Sqlite 文件

转载 作者:行者123 更新时间:2023-11-30 02:48:12 25 4
gpt4 key购买 nike

我发现了很多关于这个问题的问题,但我无法修复它。我在 Assets 文件夹中有一个 sqlite 数据库:

assets/data/data/{package_name}/databases/dbtest.sqlite

我尝试过:

  1. 卸载项目
  2. 清理项目
  3. 刷新并重启ecilipse
  4. 我检查了我的包裹名称
  5. 我检查了我的数据库文件名

但我无法解决它。我不知道我哪里错了。

提前致谢

DBHelper.java

public class DBHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/com.exasmple.dbexam/databases/";
private static String DB_NAME = "dbtest.sqlite";
public static final String APP_TABLE = "land";

private static final int DATABASE_VER5ION = 1;
private static final String TYPE = "type";
private static final String ID = "id";

private SQLiteDatabase myDataBase;
private final Context myContext;
public static String state;

public DBHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VER5ION);
this.myContext = context;
}

public void createDataBase() throws IOException {
// System.out.println("Creating database");
boolean dbExist = checkDataBase();
if (!dbExist) {
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;
}

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[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}


myOutput.flush();
myOutput.close();
myInput.close();
System.out.println("Database copied into system data");

}

public void openDataBase() throws SQLException {

System.out.println("Open database");
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);// SQLiteDatabase.NO_LOCALIZED_COLLATORS);

}

@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) {

db.execSQL("DROP TABLE IF EXISTS report");
db.execSQL("DROP TABLE IF EXISTS appstatus");
onCreate(db);
}

public ArrayList<String> GetGroupName() {
String sql = "";
// System.out.println("Inside Database");
sql = "select chapterid from content";

ArrayList<String> list = new ArrayList<String>();

Cursor cursor = myDataBase.rawQuery(sql, null);

try {
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
System.out.println("dblist value==" + list);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
} else
Toast.makeText(myContext, "No values in DB", Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
System.out.println("GetGradeNames(): " + e);
}
return list;
}

}

日志:

07-09 14:57:27.834: E/AndroidRuntime(987): FATAL EXCEPTION: main
07-09 14:57:27.834: E/AndroidRuntime(987): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exasmple.dbexam/com.exasmple.dbexam.MainActivity}: android.database.sqlite.SQLiteException: unable to open database file
07-09 14:57:27.834: E/AndroidRuntime(987): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-09 14:57:27.834: E/AndroidRuntime(987): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-09 14:57:27.834: E/AndroidRuntime(987): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-09 14:57:27.834: E/AndroidRuntime(987): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-09 14:57:27.834: E/AndroidRuntime(987): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 14:57:27.834: E/AndroidRuntime(987): at android.os.Looper.loop(Looper.java:130)
07-09 14:57:27.834: E/AndroidRuntime(987): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-09 14:57:27.834: E/AndroidRuntime(987): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 14:57:27.834: E/AndroidRuntime(987): at java.lang.reflect.Method.invoke(Method.java:507)
07-09 14:57:27.834: E/AndroidRuntime(987): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-09 14:57:27.834: E/AndroidRuntime(987): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-09 14:57:27.834: E/AndroidRuntime(987): at dalvik.system.NativeStart.main(Native Method)
07-09 14:57:27.834: E/AndroidRuntime(987): Caused by: android.database.sqlite.SQLiteException: unable to open database file
07-09 14:57:27.834: E/AndroidRuntime(987): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
07-09 14:57:27.834: E/AndroidRuntime(987): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1849)
07-09 14:57:27.834: E/AndroidRuntime(987): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
07-09 14:57:27.834: E/AndroidRuntime(987): at com.exasmple.dbexam.DBHelper.openDataBase(DBHelper.java:138)
07-09 14:57:27.834: E/AndroidRuntime(987): at com.exasmple.dbexam.MainActivity.onCreate(MainActivity.java:21)
07-09 14:57:27.834: E/AndroidRuntime(987): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-09 14:57:27.834: E/AndroidRuntime(987): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-09 14:57:27.834: E/AndroidRuntime(987): ... 11 more

最佳答案

最后我从chat room找到了答案.这可能对某人有帮助..

我更新的 DBHelper

public class DBHelper extends SQLiteOpenHelper {
public int GetCursor;
// ****************** Declare all the global variable
// ****************************//
private Context myContext;
public String DB_PATH = "data/data/com.exasmple.dbexam/databases/"; // path
// of
// your
// datbase
public static String DB_NAME = "Test.sqlite";// your database name
static String ASSETS_DB_FOLDER = "db";
private SQLiteDatabase db;

public DBHelper(Context context) {
super(context, DB_NAME, null, 2);
if (db != null && db.isOpen())
close();

this.myContext = context;
//DB_NAME = db_name;

try {
createDataBase();
openDataBase();
} catch (IOException e) {
// System.out.println("Exception in creation of database : "+
// e.getMessage());
e.printStackTrace();
}

}



public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();

if (dbExist) {
// System.out.println("Database Exist");
} else {
this.getReadableDatabase();

try {
copyDatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}

private void copyDatabase() throws IOException {
InputStream input = myContext.getAssets().open(DB_NAME);
String outputFileName = DB_PATH + DB_NAME;
OutputStream output = new FileOutputStream(outputFileName);

byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}

// Close the streams
output.flush();
output.close();
input.close();
// System.out.println(DB_NAME + "Database Copied !");
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}

public boolean isOpen() {
if (db != null)
return db.isOpen();
return false;
}

@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}

private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
// System.out.println("My Pathe is:- " + myPath);
// System.out.println("Open");
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
// System.out.println("checkDB value:" + checkDB);
// System.out.println("My Pathe is:- " + myPath);
} catch (Exception e) {
// database does't exist yet.
}

if (checkDB != null) {
// System.out.println("Closed");
checkDB.close();
// System.out.println("My db is:- " + checkDB.isOpen());
}

return checkDB != null ? true : false;
}

public Cursor execCursorQuery(String sql) {
Cursor cursor = null;
try {
cursor = db.rawQuery(sql, null);
GetCursor = cursor.getCount();
Log.i("Inside execCursorQuery try", sql);
} catch (Exception e) {
Log.i("Inside execCursorQuery exception", e.getMessage());
}
return cursor;
}

public void execNonQuery(String sql) {
try {
db.execSQL(sql);
// Log.d("SQL", sql);
} catch (Exception e) {
// Log.e("Err", e.getMessage());
} finally {
// closeDb();
}
}

public ArrayList<String> GetchapterID(){
String sql="";
//System.out.println("Inside Database");
sql="select distinct(ChapterId) from contents";

ArrayList<String> list = new ArrayList<String>();
Cursor cursor=db.rawQuery(sql, null);

try{
if(cursor != null){
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
else
Toast.makeText(myContext, "No values in DB", Toast.LENGTH_SHORT).show();
}
catch(Exception e){
System.out.println("GetGradeNames(): " +e);
}
return list;
}
}

关于android - 无法打开数据库 Sqlite 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24651538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com