我的Android应用程序中存在数据库连接问题。奇怪的是,一切都运转良好。一个晴朗的早晨,我开始遇到这个问题。并且还有现有的运行良好的应用程序。
我认为这是一种环境问题,但无法追踪它。需要你帮助的人。我查看了SOF中的现有帖子。尝试了所有建议,没有任何帮助。
我的数据库连接程序:
package com.intw.jokes;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseUtil extends SQLiteOpenHelper{
public SQLiteDatabase DB;
public String DBPath;
public static String DBName = "JOKES";
public static final int version = '1';
public static Context currentContext;;
public DatabaseUtil(Context context) {
super(context, DBName, null, version);
currentContext = context;
DBPath = "/data/data/" + context.getPackageName() + "/databases/";
}
public SQLiteDatabase getDatabaseObject(){
return this.getWritableDatabase();
}
public void closeDBConnection(SQLiteDatabase DB){
if(null != DB)
DB.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase() {
boolean dbExists = checkDbExists();
//dbExists = false;
if (dbExists) {
// do nothing
} else {
DB = currentContext.openOrCreateDatabase(DBName, 0, null);
DB.close();
}
}
private boolean checkDbExists() {
SQLiteDatabase checkDB = null;
try {
String myPath = DBPath + DBName;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
}
boolean test = checkDB != null ? true : false;
if (checkDB != null) {
checkDB.close();
}
return test;
}
}
和我的日志:
03-21 10:13:56.844: E/Trace(780): error opening trace file: No such file or directory (2)
03-21 10:13:59.794: E/SQLiteLog(780): (14) cannot open file at line 30174 of [00bb9c9ce4]
03-21 10:13:59.794: E/SQLiteLog(780): (14) os_unix.c:30174: (2) open(/data/data/com.intw.jokes/databases/JOKES) -
03-21 10:13:59.834: E/SQLiteDatabase(780): Failed to open database '/data/data/com.intw.jokes/databases/JOKES'.
03-21 10:13:59.834: E/SQLiteDatabase(780): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
03-21 10:13:59.834: E/SQLiteDatabase(780): at com.intw.jokes.DatabaseUtil.checkDbExists(DatabaseUtil.java:111)
03-21 10:13:59.834: E/SQLiteDatabase(780): at com.intw.jokes.DatabaseUtil.createDatabase(DatabaseUtil.java:49)
03-21 10:13:59.834: E/SQLiteDatabase(780): at com.intw.jokes.AllJokesActivity.configureDatabase(AllJokesActivity.java:201)
03-21 10:13:59.834: E/SQLiteDatabase(780): at com.intw.jokes.AllJokesActivity.onCreate(AllJokesActivity.java:80)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.app.Activity.performCreate(Activity.java:5008)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.os.Looper.loop(Looper.java:137)
03-21 10:13:59.834: E/SQLiteDatabase(780): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-21 10:13:59.834: E/SQLiteDatabase(780): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 10:13:59.834: E/SQLiteDatabase(780): at java.lang.reflect.Method.invoke(Method.java:511)
03-21 10:13:59.834: E/SQLiteDatabase(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-21 10:13:59.834: E/SQLiteDatabase(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 10:13:59.834: E/SQLiteDatabase(780): at dalvik.system.NativeStart.main(Native Method)
03-21 10:14:00.534: D/dalvikvm(780): GC_CONCURRENT freed 304K, 9% free 7416K/8135K, paused 16ms+27ms, total 130ms
03-21 10:14:00.534: D/dalvikvm(780): WAIT_FOR_CONCURRENT_GC blocked 11ms
03-21 10:14:01.894: D/gralloc_goldfish(780): Emulator without GPU emulation detected.
答案 0 :(得分:1)
如果存在,还有另一种方法可以检查数据库。正如Andre Rocha所回答的那样,在checkDbExists()
内检查数据库,不需要像现在这样复杂,
private boolean checkDbExists() {
File file= currentContext.getDatabasePath(DBName);
return file.exists();
}