2013-08-29 48 views
0

我剛接觸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) 
+0

發佈logcat。 –

+0

@Ritesh Gune增加了logcat。 – Bruno

回答

0

哦,原來,這個問題很簡單和愚蠢:我只是複製我的壓縮數據庫,以資產文件夾原來應該在資產/數據庫複製用於SQLiteAssetHelper上班的時候。

創建文件夾並將數據庫放入其中後,一切正常。

-1

您可以加入的logcat的輸出爲什麼正在崩潰?

你的數據庫名稱是contacts.db,不是你的表名

請設置

String sqlTables = "your_table_name"; 

編輯正確的表名: 我儘量給大家簡單介紹一下成在Android中使用SQLite就像我在我的一個項目中做的那樣。

首先,你有一個DBHandler類:

public class DBHandler extends SQLiteOpenHelper { 

    // table names 
    public static final String TABLE_NAME = "contacts"; 

    // table column names 
    public static final String COLUMN_ID = "id"; 
    public static final String COLUMN_NAME = "name"; 
    public static final String COLUMN_NUMBER = "number"; 

    // Database Name 
    private static final String DATABASE_NAME = "contacts"; 
    private static final int DATABASE_VERSION = 1; 

    // Statements for creating tables 
    private static final String TABLE_CREATE = "CREATE TABLE " 
      + TABLE_NAME + " (" + COLUMN_ID 
      + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + COLUMN_NAME 
      + " VARCHAR(255)," + COLUMN_NUMBER + " VARCHAR(255);"; 

    public DBHandler(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    // Create tables 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(TABLE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(DBHandler.class.getName(), 
       "Upgrading database from version " + oldVersion + " to " 
         + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db); 
    } 
} 

然後你需要處理您的查詢和其他的東西一類...可以調用它的DataSource:

public class DataSource { 

// Database fields 
private SQLiteDatabase database; 
private DBHandler databaseHelper; 
private String[] allColumns = { DBHandler.COLUMN_ID, 
     DBHandler.COLUMN_NAME, DBHandler.COLUMN_NUMBER}; 

public DataSource(Context context) { 
    databaseHelper = new DBHandler(context); 
     this.open(); 
} 

public void open() throws SQLException { 
    database = databaseHelper.getWritableDatabase(); 
} 

public void close() { 
    databaseHelper.close(); 
} 

    // here you can place your custom functions 
    public Cursor getEmployees(){ 
      Cursor cursor = database.query(DBHandler.TABLE_NAME, 
      allColumns, null, null, null, null, null); 
      cursor.moveToFirst(); 
      return cursor; 
    } 
} 

在你MainActivity你做:

public class MainActivity extends ListActivity { 

     private Cursor employees; 
     private DataSource datasource; 
     private SimpleCursorAdapter adapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     datasource = new DataSource(this); 
     employees = datasource.getEmployees(); 

     adapter = new SimpleCursorAdapter(this, 
     android.R.layout.simple_list_item_1, 
     employees, 
     new String[] {"name"}, 
     new int[] {android.R.id.text1},0); 

     this.setListAdapter(adapter); 
    } 

    @Override 
    public void onResume() { 
     datasource.open(); 
     super.onResume(); 
    } 

    @Override 
    public void onPause() { 
     datasource.close(); 
     super.onPause(); 
    } 
} 

您的佈局應該是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 
    <ListView 
     android:id="@android:id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

重要的是ListView的id是android:list。

我沒有測試這個特定的,但我調整了我的代碼。它應該像那樣工作。

+0

我的數據庫和表都稱爲「聯繫人」,但我從表名中刪除了.db。仍然沒有變化。 – Bruno

+0

在對代碼做了一些調整之後,它對我有用,但它只是創建一個空數據庫,我仍然無法從資產文件夾中的數據庫中複製數據。 – Bruno

+0

你必須填寫數據 – MKAI