2013-07-22 49 views
0

我有一個現有的sqlite數據庫。我正在開發一個android應用程序,我想將它與現有的sqlite數據庫連接起來。從現有的sqlite數據庫提取數據

問題1: 我已經包括通過「DDMS推數據庫功能」按我的教練的提醒在我的項目SQLite數據庫。現在我想從數據庫中獲取數據,我是否需要使用SQLiteOpenHelper。如果是的話,如何使用它,以及將在onCreate(SQLiteDatabase db)函數和onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)函數中編碼的內容,因爲我們已經有了數據庫,我們並不需要創建它。

問題2: 應該怎樣做簡單地從現有的數據庫中獲取所需的數據。作爲一個新手,我很困惑,有人可以解釋這些概念,並指導我解決這個問題。任何幫助,將不勝感激。

我已經看到一個教程也爲此目的作爲@TronicZomB sugggested,但根據本教程(http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/),我必須讓主鍵字段爲_id的所有表。

我有7個表,即目的地,事件,旅遊,tour_cat,tour_dest,android_metadata和sqlite_sequence。總而言之,只有tour_dest不符合將主鍵命名爲_id的條件。如何弄清楚這一點?

以下是缺少數據庫表綁定ID字段所需的主鍵字段的表的截圖。 Screenshot of table which is lacking the primary key field necessary for binding id fields of database tables.

回答

4

onCreateonUpgrade方法將爲空,因爲您已經擁有該數據庫。有一個偉大的教程,如何實現這個here

然後,您可以訪問像這樣的(例如)數據庫:

public ArrayList<String> getValues(String table) { 
    ArrayList<String> values = new ArrayList<String>(); 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery("SELECT value FROM " + table, null); 

    if(cursor.moveToFirst()) { 
     do { 
      values.add(cursor.getString(cursor.getColumnIndex("value"))); 
     }while(cursor.moveToNext()); 
    } 

    cursor.close(); 
    db.close(); 
    return values; 
} 
+0

我已經看到,嘖嘖,但我有一個關於一個小問題。它說所有的表都必須有一個字段_id。但是我有一張不具備主鍵字段的表格。根據教程,我需要一個名爲_id的主鍵字段。現在,我不得不重新創建將丟失數據的表。該怎麼辦.. – divyang7

+0

嗯...我用這個教程,並在我的ID列在一個表中作爲「ID」和我的其他表沒有一個主鍵整數,它適用於我... – TronicZomB

+0

我編輯了我的問題,幷包括一個圖像也描述當前缺少主鍵_id的表的結構。休息所有的表都有一個主鍵_id,從而滿足您提供的教程條件。 – divyang7

1

除非你是非常舒適的查詢,數據庫等我強烈建議你使用http://satyan.github.io/sugar/,它還將刪除了很多鍋爐板碼需要在Android中做sqlite

1

1.如果DB已經存在,onCreate將不會調用。僅當您更改數據庫版本時,纔會調用onUpgradeonUpgrade如果您的APP數據庫發生了一些變化,您應該使用它,並且您必須順利地遷移新的數據結構。

public class DbInit extends SQLiteOpenHelper { 
    private static final String DATABASE_NAME = "name"; 
    private static final int DATABASE_VERSION = 3; 
    private static final String DATABASE_CREATE = "create table connections . .. . ... 

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

    @Override 
    public void onCreate(SQLiteDatabase database) { 
     database.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     if (isChangeFromToVersion(1, 2, oldVersion, newVersion)) { 
      //Execute UPDATE here 
     } 
    } 

    private boolean isChangeFromToVersion(int from, int to, int oldVersion, int newVersion) { 
     return (from == oldVersion && to == newVersion); 
    } 
.... 

2.簡單的例子如何打開連接到數據庫並獲取光標對象。

公共類DAO {

private SQLiteDatabase database; 
private DbInit dbHelper; 

public ConnectionDAO(Context context) { 
    dbHelper = new DbInit(context); 
} 

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

public Connection getConnectionById(long id) { 
    Cursor cursor = null; 
    try { 
     open(); 
     cursor = database.query(DbInit.TABLE_CONNECTIONS, allColumns, DbInit.COLUMN_ID + " = '" + id + "'", null, null, null, null); 
     if (!cursor.moveToFirst()) 
      return null; 
     return cursorToConnection(cursor); 
    } finally { 
     if (cursor != null) 
      cursor.close(); 
     close(); 
    } 
} 

private Connection cursorToConnection(Cursor cursor) { 
    Connection connection = new Connection(); 
    connection.setId(cursor.isNull(0) ? null : cursor.getInt(0)); 
    connection.setName(cursor.isNull(1) ? null : cursor.getString(1)); 
    ..... 
    ..... 
    return connection; 
}