从SQLite的选择数据到ListArrays [英] Select data from SQLite into ListArrays

查看:249
本文介绍了从SQLite的选择数据到ListArrays的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经伤透我对这个大脑的天,我只是不能换我的头周围在Android中/ Java中使用SQLite数据库。我试图从SQLite数据库两行成ListArray(或两种,一种为每行。如果没有把握会更好或更糟),我只是不知道如何去做。我试过,我已经发现的各种数据库管理器类,但他们没有做什么,我需要和看来,我应该能够做到这一点简单的任务没有我在其他数据库管理器看到的额外功能。有没有简单的方法,只是从现有的SQLite数据库查询一些数据,并把它变成一个ListArray,这样我可以使用它?我知道我需要将数据库从资产复制到Android的数据库路径,我能处理的那部分。我还需要能够修改每行的列中的一个。我并不需要创建数据库或表或行。恳请有人帮我这个因为我认为任何code我写(从网上复制)是完全无用的。

I've been racking my brain on this for days and I just can't wrap my head around using SQLite databases in Android/Java. I'm trying to select two rows from a SQLite database into a ListArray (or two, one for each row. Not sure if that would be better or worse) and I just don't understand how to do it. I've tried various database manager classes that I've found but none of them do what I need and it seems that I should be able to do this simple task without the extra features I've seen in other database managers. Is there any simple way to JUST query some data from an existing SQLite database and get it into a ListArray so that I can work with it? I realize I need to copy the database from assets into the Android database path and I can handle that part. I also need to be able to modify one of the columns per row. I don't need to create databases or tables or rows. I implore someone to help me with this as I consider any code I've written (copied from the internet) to be completely useless.

推荐答案

您可以创建一个方法是这样的:

You can create a method like this :

private List<MyItem> getAllItems()
    List<MyItem> itemsList = new ArrayList<MyItem>();
    Cursor cursor = null;
    try {
        //get all rows
        cursor = mDatabase.query(MY_TABLE, null, null, null, null,
                null, null);
        if (cursor.moveToFirst()) {
            do {
                MyItem c = new MyItem();
                c.setId(cursor.getInt(ID_COLUMN));
                c.setName(cursor.getString(NAME_COLUMN));
                itemsList.add(c);
            } while (cursor.moveToNext());
        }
    } catch (SQLiteException e) {
        e.printStackTrace();
    } finally {
        cursor.close();
    }
    return itemsList;
}

这将是你的类里面让我们说 MyDatabaseHelper ,你还必须声明:

This will be inside your class let say MyDatabaseHelper where you will also have to declare a :

private static class DatabaseHelper extends SQLiteOpenHelper{

    private final static String DATABASE_CREATE="create table " + MY_TABLE + " (id integer primary key, country string);";

    public DatabaseHelper(Context context,String name, CursorFactory factory, int version){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

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

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

用于的open()的close()数据库。

这篇关于从SQLite的选择数据到ListArrays的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆