简体   繁体   English

从现有的sqlite数据库中获取数据

[英]Fetch data from existing sqlite database

I am having an existing sqlite database. 我有一个现有的sqlite数据库。 I am developing an android app and I want to connect it with this existing sqlite DataBase. 我正在开发一个android应用程序,我想将其与此现有的sqlite数据库连接。

Problem 1: I have already included the sqlite database in my project via "DDMS push database function" as per my instructor's advise. 问题1:我已经按照指导老师的建议通过“ DDMS推送数据库功能”将sqlite数据库包含在我的项目中。 Now I want to fetch the data from database, do I need to use SQLiteOpenHelper. 现在,我想从数据库中获取数据,是否需要使用SQLiteOpenHelper。 If yes, how to use it and what will be coded in onCreate(SQLiteDatabase db) function and onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) function as we already have the Database with us, we don't really need to create it. 如果是的话,如何使用它以及将在onCreate(SQLiteDatabase db)函数和onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)函数中编码的内容,因为我们已经有了数据库,所以我们实际上不需要创建它。

Problem 2: What should be done to simply fetch the required data from the existing database. 问题2:应该如何做才能从现有数据库中简单地获取所需数据。

Being a newbie, I am quite confused, can someone please explain these concepts and guide me to overcome this issue. 作为一个新手,我很困惑,有人可以解释一下这些概念并指导我克服这个问题。 Any help would be appreciated. 任何帮助,将不胜感激。

I have seen a tutorial also for this purpose as sugggested by @TronicZomB, but according to this tutorial ( http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ ), I must be having all the tables with primary key field as _id. 我也看到了@TronicZomB建议的一个为此目的的教程,但是根据该教程( http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ ) ,我必须使用主键字段为_id的所有表。

I have 7 tables namely destinations, events, tour, tour_cat, tour_dest, android_metadata and sqlite_sequence. 我有7个表,分别是目的地,事件,游览,tour_cat,tour_dest,android_metadata和sqlite_sequence。 Out of all, only tour_dest is not fulfilling the conditions of having a primary key named as _id. 总而言之,只有tour_dest不能满足拥有名为_id的主键的条件。 How to figure out this one? 如何找出这一点?

Following is the screenshot of table which is lacking the primary key field necessary for binding id fields of database tables. 以下是表的屏幕快照,该屏幕缺少绑定数据库表的id字段所需的主键字段。 表的屏幕快照,该屏幕缺少绑定数据库表的id字段所需的主键字段。

The onCreate and onUpgrade methods will be empty since you already have the database. 由于您已经拥有数据库,因此onCreateonUpgrade方法将为空。 There is a great tutorial on how to achieve this here . 对于如何实现这个伟大的教程在这里

You could then access the database like such (example): 然后,您可以像这样访问数据库(示例):

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;
}

除非您对查询,数据库等非常满意,否则我强烈建议您使用http://satyan.github.io/sugar/ ,它还会删除很多在Android中执行sqlite所需的样板代码

1. If DB already exists, onCreate will not invoke. 1.如果DB已经存在,则onCreate将不会调用。 onUpgrade will be invoked only if you will change DB version. 仅当您更改数据库版本时,才会调用onUpgrade onUpgrade you should to use if there some changes in your APP's database, and you have to make migration on new structure of data smoothly. 如果APP的数据库中有一些更改,则应使用onUpgrade ,并且必须平稳地在新的数据结构上进行迁移。

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. Simple example how to open connection to DB and get cursor object. 2.一个简单的示例,说明如何打开与DB的连接并获取游标对象。

public class DAO { 公共课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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM