如何从数据库中获取所有值

时间:2014-12-28 19:10:36

标签: android sqlite

您好我正在使用SQLite,我已经将所有数据都插入到数据库中,我该怎样才能进行检索?
这是我的插入方法

public void insert(String username, String password, String email) {
    ContentValues values = new ContentValues();
    values.put(UsersSQLiteHelper.COLUMN_UNAME, username);
    values.put(UsersSQLiteHelper.COLUMN_PASS, passward);
    values.put(UsersSQLiteHelper.COLUMN_EMAIL, email);
    long insertId = database.insert(UsersSQLiteHelper.TABLE_USERS, null,values);
}

1 个答案:

答案 0 :(得分:1)

public List<YOUROBJECT> getAllInstruments() {
    // create a list
    List<YOUROBJECT> list = new ArrayList< YOUROBJECT>();

    Cursor cursor = database.query(UsersSQLiteHelper.TABLE_USERS,
    allColumns, null, null, null, null, null);

    // check if cursor has columns
    if (cursor.getColumnCount() > 0) {
    // read all cursor values
    while (cursor.moveToNext()) {
        YOUROBJECT newObject = new YOUROBJECT();
        // now you have to get the values from the cursor and add them to your object
        // like this
        String userName= cursor.getString(cursor.getColumnIndex(COLUMN_UNAME));
        // COLUMN_UNAME is the name of the column in your database that you want the value of

        // now add the values to the Instrument object
        newObject.setUserName(userName);

        // now add the object to the list
        list.add(newObject);
    }
}

cursor.close();
return list; //finally return the list

}