在SQLite数据库Android Studio上使用预填充数据库

Using Pre-populated Database on SQLite database Android Studio

本问题已经有最佳答案,请猛点这里访问。

在这里,我想使用我预先填充的SQLite数据库到我的Android应用程序。 因此,首次运行时,它会自动从assets文件夹中复制数据库,并在我的Android应用程序中将其用作数据库。

到目前为止,我已经做了这些事:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String MyVillageSoftware ="MyVillageSoftware";
public static final String table_Question ="table_question";
private static final String DATABASE_PATH="/Users/ever_ncn/Documents/Project/PsikologiKarakter/app/src/main/assets/";
private static final String DATABASE_NAME="question.db";
//TOL for transaction Coloumn
SQLiteDatabase db;
Context context;
private static final String TAG = DatabaseHelper.class.getSimpleName();



public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    this.context=context;

}

@Override
public void onCreate(SQLiteDatabase db) {

}

public void createDatabase(){
    createDB();
}

public void createDB(){
    boolean dbExist = DBExist();

    if(!dbExist){
        this.getReadableDatabase();
        copyDBFromResource();
    }

}

private boolean DBExist(){
    SQLiteDatabase db = null;
    try{
        String databasePath = DATABASE_PATH+DATABASE_NAME;
        db=SQLiteDatabase.openDatabase(databasePath, null,
                SQLiteDatabase.OPEN_READWRITE);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
        db.setVersion(1);
    }catch(SQLiteException e){
        Log.e("SqlHelper","Database nda ada babi");
    }
    if(db != null){
        db.close();
    }return db != null ? true : false;
}

private void copyDBFromResource(){
    InputStream inStream=null;
    OutputStream outStream = null;
    String dbFilePath=DATABASE_PATH+DATABASE_NAME;
    try{
        inStream=context.getAssets().open(DATABASE_NAME);
        outStream=new FileOutputStream(dbFilePath);
        byte[] buffer = new byte[1024];
        int length;
        while((length=inStream.read(buffer))>0){
            outStream.write(buffer, 0 , length);
        }
        outStream.flush();
        outStream.close();
        inStream.close();
    }catch (IOException e){
        throw new Error("Problem cuk");
    }
}

public List<String> getAllCategory() {
    List<String> AllCategoryList = new ArrayList<String>();
    List<String> AllCategIdList = new ArrayList<String>();

    String selectQuery ="SELECT * FROM" + table_Question;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);



    if (cursor.moveToFirst()) {
        do {
            String Id = cursor.getString(0);
            String A = cursor.getString(1);
            String B = cursor.getString(2);
            String C = cursor.getString(3);
            String D = cursor.getString(4);
            String aExplain = cursor.getString(5);
            String bExplain = cursor.getString(6);
            String cExplain = cursor.getString(7);
            String dExplain = cursor.getString(8);

        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return AllCategoryList;
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + table_Question);
    onCreate(db);

}

}

但我得到一个这样的错误信息:

10-28 07:18:38.988 2425-2425/gook.psikologikarakter E/SQLiteLog﹕ (14) cannot open file at line 30046 of [9491ba7d73]
10-28 07:18:38.988 2425-2425/gook.psikologikarakter E/SQLiteLog﹕ (14) os_unix.c:30046: (2) open(/Users/ever_ncn/Documents/Project/PsikologiKarakter/app/src/main/assets/question.db) -
10-28 07:18:38.989 2425-2425/gook.psikologikarakter E/SQLiteDatabase﹕ Failed to open database '/Users/ever_ncn/Documents/Project/PsikologiKarakter/app/src/main/assets/question.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
at gook.psikologikarakter.DatabaseHelper.DBExist(DatabaseHelper.java:65)
at gook.psikologikarakter.DatabaseHelper.createDB(DatabaseHelper.java:52)
at gook.psikologikarakter.DatabaseHelper.createDatabase(DatabaseHelper.java:48)
at gook.psikologikarakter.MainActivity.onCreate(MainActivity.java:59)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我不知道到底发生了什么,但我想我无法打开数据库文件。 但我不知道该怎么办。 请高手帮帮我。

谢谢你。


1
private static final String DATABASE_PATH="/Users/ever_ncn/Documents/Project/PsikologiKarakter/app/src/main/assets/";

这是本地文件系统上的路径,而不是运行时设备上文件的路径。 使用getResources().getAssets().openFile(...)获取文件的InputStream