我是 android 编程的新手,我试图用集成的 SQLite 数据库制作一个应用程序。
在多次尝试编写将数据库从资产文件夹复制到内部存储器的代码后,我偶然发现了这个SQLiteAssetHelper库并尝试使用它,但它只是打开了空白的应用程序屏幕,几秒钟后,它崩溃了显示消息“(AppName) 已停止。”
我在一个真实的设备上测试我的应用程序,一个无根的三星 Galaxy S3。
一些代码:
主要活动
package com.example.database2;
import android.os.Bundle;
import android.app.ListActivity;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListAdapter;
public class MainActivity extends ListActivity {
private Cursor employees;
private Database db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new Database(this);
employees = db.getEmployees();
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
employees,
new String[] {"name"},
new int[] {android.R.id.text1},0);
getListView().setAdapter(adapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
employees.close();
db.close();
}
}
SQLite 资产助手
package com.example.database2;
import android.content.Context;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
public class Database extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "contacts.db";
private static final int DATABASE_VERSION = 1;
public Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public Cursor getEmployees() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String [] sqlSelect = {"_id", "name", "number"};
String sqlTables = "contacts.db";
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, null, null,
null, null, null);
c.moveToFirst();
return c;
}
}
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.database2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.database2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
很高兴有人可以提供帮助。
编辑:
我发布了相关的 logcat,虽然它没有给我太多信息:
日志猫
E/SQLiteDatabase(3658): Failed to open database '/data/data/com.example.database2/databases/contacts'.
E/SQLiteDatabase(3658): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
E/SQLiteDatabase(3658): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
E/SQLiteDatabase(3658): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:278)