2013-07-22 46 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; 
}