预填充的数据库-Android应用程序 [英] Prepopulated database - Android application

查看:66
本文介绍了预填充的数据库-Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建具有预先填充的数据库的android应用程序时,我确实遇到了麻烦.

请查看整个 的代码. >

我通过使用sqlitebrowser创建了一个数据库,并将其复制到了我的Android应用程序中的资产"目录.

我添加了从Java类DBAdapter.java

中的Assets目录中复制数据库的代码.

MainActivity.java中,我调用了createDatabase函数,因此,如果数据库不存在,则应从资产目录复制数据库.创建数据库后,我调用getAllContacts函数并将其显示在页面上.

每当我执行项目时,它都会引发我无法调试的错误.

有人可以帮我解决问题吗?

更新:

Logcat显示以下异常.似乎没有复制/创建资产/MyDB.db中的联系人"表,但我不确定为什么.

E/AndroidRuntime(  672): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.me.mydb/org.me.mydb.MainActivity}: android.database.sqlite.SQLiteException: no such table: contacts: , while compiling: SELECT _id, name, email FROM contacts
E/AndroidRuntime(  672):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(  672):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  672):        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  672):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  672):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  672):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  672):        at android.app.ActivityThread.main(ActivityThread.java:4627)

解决方案

source code.

I created a database by making use of sqlitebrowser and copied it the "assets" directory in my Android application.

I added the code of copying the database from assets directory in a Java Class DBAdapter.java

In MainActivity.java I called createDatabase function so if the database doesn't exist it should copy the database from assets directory. After creating the database I call the getAllContacts function and display it on the page.

Whenever I execute the project it throws an error which I'm not able to debug.

Could anyone please help me out to resolve the issue?

UPDATE:

Logcat shows the following exception. It seems "contacts" table in assets/MyDB.db is not copied/created but I'm not sure why.

E/AndroidRuntime(  672): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.me.mydb/org.me.mydb.MainActivity}: android.database.sqlite.SQLiteException: no such table: contacts: , while compiling: SELECT _id, name, email FROM contacts
E/AndroidRuntime(  672):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(  672):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  672):        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  672):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  672):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  672):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  672):        at android.app.ActivityThread.main(ActivityThread.java:4627)

解决方案

Here is a good link to get your started.

Essentially the idea is to use the SQLiteOpenHelper class and when no database exists you copy a your pre-seeded database byte for byte into the correct location. I have it in a project of mine slightly modified but it works appears to work just fine

这篇关于预填充的数据库-Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆