无法打开SQLite数据库,但不知道为什么 [英] Can't open SQLite Database but no clue why
本文介绍了无法打开SQLite数据库,但不知道为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以这是我在使用 sqlite的
数据库与Android第一枪。我已经尝试了很多东西,使其工作,但它只是将无法打开D ** N数据库。
So this is my first shot at using a sqlite
database with Android. I've tried a lot of things to make it work but it just won't open the d**n database.
数据库文件
是 RES /资产
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
从日志我知道这么多:
From the log I know this much:
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
这是code的有趣的部分:
This is the interesting part of the code:
package de.minor.quizproto.database;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MinorDbHelper extends SQLiteOpenHelper{
private final Context myContext;
public MinorDbHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
this.myContext = context;
DATABASE_PATH = myContext.getDatabasePath(DATABASE_FILE).getPath();
}
private SQLiteDatabase database;
private static final String DATABASE_NAME = "minor";
private static final int DATABASE_VERSION = 1;
private static String DATABASE_PATH;
public static String DATABASE_FILE = "minor.db";
private static final String TABLE_QUIZ = "quiz";
private static final String TABLE_STATS = "stats";
private static final String TABLE_FRIENDLIST = "friendlist";
private static final String TABLE_FRIENDSTAT = "friendstat";
private static final String ID = "_id";
private static final String QUESTION = "question";
private static final String ANSWER1 = "answer1";
private static final String ANSWER2 = "answer2";
private static final String ANSWER3 = "answer3";
private static final String ANSWER4 = "answer4";
private static final String CORRECT = "correct";
private static final String LEVEL = "level";
private static final String QUIZ_ID = "quizid";
private static final String TOPIC_ID = "topicid";
private static final String TAG = "helper";
//hoping copying the database helpwould help
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DATABASE_FILE);
// Path to the just created empty db
String outFileName = DATABASE_FILE;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
String myPath = DATABASE_PATH;
Log.d("MinorDbHelper", myPath);
//THAT'S THE LINE WHERE THE EXCEPTION OCCURS:
database = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
唯一的例外在此行中出现:
The exception comes up in this line:
database = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
任何想法是怎么回事?我试过的USB调试和安装APK - 同样的结果...
Any idea what's going wrong? I've tried USB-Debugging and installing the apk - same result...
非常感谢您的任何建议。
Thanks a lot for any suggestions
推荐答案
您在这里需要改变某些事情了。
You need to change something here
public String DB_PATH = "data/data/yourPkgNameHere/databases/"; // path
public static String DB_NAME = "minor.db";// your database name
static String ASSETS_DB_FOLDER = "db";
public MinorDbHelper(Context context, String db_name) {
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 !");
}
这篇关于无法打开SQLite数据库,但不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文