SQLite查询数据从游标中检索

时间:2012-06-05 09:35:07

标签: android sqlite

我的UI中有2个编辑框。我想从表中检索数据,我想将检索到的数据插入到那些编辑文本框中,如何将数据从光标插入到这些编辑文本框中?

2 个答案:

答案 0 :(得分:1)

检查你的号码。列的名称及其名称cursor.getColumnCount()cursor.getColumnName(0).。如果列数为2,则光标有两列

cursor.moveToFirst();

String columnName1 = cursor.getColumnName(0);
String columnName2 = cursor.getColumnName(1);    

String str1 = cursor.getString(cursor.getColumnIndex(columnName1)));
String str2 = cursor.getString(cursor.getColumnIndex(columnName2))); 

editext1.seText(str1);   
editext2.seText(str2); 

完成从数据库获取数据后,使用cursor.close();

关闭光标

答案 1 :(得分:0)

// Activity.onCreate function

EditText etfirstname= (EditText)findViewById(R.id.firstname);
EditText etlastname= (EditText)findViewById(R.id.lastname);
MyDatabase database = new MyDatabase(this);

Cursor c = database.queryRaw("SELECT firstname, lastname FROM users WHERE id=1"); // query data from database
if(c.moveToFirst()){
    etfirstname.setText(c.getString(0)); // read firstname
    etlastname.setText(c.getString(1)); // read lastname
}

c.close(); // dont forget to close cursor!