我剛接觸android編程,並試圖用集成的SQLite數據庫創建應用程序。集成SQLite數據庫的應用程序不斷崩潰
編寫從資產文件夾到內部存儲器複製DB,我偶然發現了這個SQLiteAssetHelper庫,並試圖使用它的代碼的許多失意嘗試後,但它只是打開空白的應用程序屏幕,後幾秒鐘,它崩潰並顯示消息「(AppName)已停止。」
我正在測試我的應用程序在一個真正的設備,一個沒有根的三星Galaxy S3。
一些代碼:
MainActivity
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>
很高興如果有人可以幫忙。
編輯:
I'm發佈有關logcat的,雖然它doesn't給了我太多的信息:
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)
發佈logcat。 –
@Ritesh Gune增加了logcat。 – Bruno