无法打开SQLite数据库但不知道原因

时间:2014-03-28 11:08:07

标签: android android-sqlite

所以这是我第一次使用Android sqlite数据库。我已经尝试了很多东西来使它工作,但它不会打开d ** n数据库。

数据库file位于res/assets

权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

从日志中我知道了很多:

android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not  open database

这是代码中有趣的部分:

    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);
    }

此行中出现例外情况:

database = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

知道出了什么问题吗?我尝试过USB-Debugging并安装apk - 结果相同......

非常感谢任何建议

3 个答案:

答案 0 :(得分:1)

你的代码看起来很好看。我观察到的一个奇怪的事情是你在res文件夹中创建你的资产文件夹......这可能是你的代码无法找到/打开数据库的原因。 assets文件夹必须位于src,res,bin等文件夹的同一级别。尝试移动该文件夹,看看调用

时会发生什么
 myContext.getAssets().open(DATABASE_FILE);

答案 1 :(得分:1)

您正在尝试将数据库从资产写入资产

 InputStream myInput = myContext.getAssets().open(DATABASE_FILE);
 String outFileName = DATABASE_FILE;
 OutputStream myOutput = new FileOutputStream(outFileName);

InputStream和OutputStream指向同一个位置,它应该是

 InputStream myInput = myContext.getAssets().open(DATABASE_FILE);
 OutputStream myOutput = new FileOutputStream(DATABASE_PATH);

答案 2 :(得分:1)

你需要在这里改变一些东西

 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 !");
}