我发现了很多关于这个问题的问题,但我无法修复它。我在资源文件夹中有一个sqlite数据库:
assets/data/data/{package_name}/databases/dbtest.sqlite
我试图:
但我无法解决它。我不知道我哪里错了。
先谢谢
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;
}
}
Logcat:
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
答案 0 :(得分:1)
最后我找到了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;
}
}
答案 1 :(得分:0)
close
与进程A 数据库建立连接。 concurrency
。db.close();
关闭此类连接答案 2 :(得分:0)
DB_NAME应该是 -
DB_NAME="dbtest";
对于DB_PATH,您使用了包com.exasmple.dbexam
,
DB_PATH = "/data/data/com.exasmple.dbexam/databases/";
可能是错的。
注意: 输入正确的软件包名称(您可以从清单文件中查看)。
<强>更新强>
喜欢 -
public void createDataBase() throws IOException {
// System.out.println("Creating database");
boolean dbExist = checkDataBase();
if (dbExist == true) {
this.getReadableDatabase();
try {
this.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
答案 3 :(得分:0)
打开位于assets文件夹中的数据库文件。总是正确答案的要点是:databases located in assets cannot be opened
因为这样的文件夹(作为APK的整个内容)是只读的。
但是,如果实际上不必修改数据库文件会怎么样?在这种情况下,资产文件夹是只读的限制并不重要。
所以将你的db文件复制到sdcard,data\data folder
之类的其他位置,然后它就是可读的
答案 4 :(得分:0)
试试这个,它会对你有帮助: -
http://blog.softeq.com/2012/12/using-pre-populated-sqlite-database-in.html
从资源文件夹中取出数据库文件并使用sqlite浏览器插入数据并再次将该文件粘贴到该文件夹中
注意: - 不要忘记根据你改变列名。
答案 5 :(得分:0)
尝试更改此内容:
private static String DB_NAME = "dbtest.sqlite";
这个
private static String DB_NAME = "dbtest.db";