praveen voggu
praveen voggu

Reputation: 47

SQLite Query data retrieve from cursor

I have 2 edit boxes in my UI. I want to retrieve data from a table and I want to insert those retrieved data into those edit text boxes how can I insert data into those edit text boxes from cursor?

Upvotes: 0

Views: 343

Answers (2)

Samir Mangroliya
Samir Mangroliya

Reputation: 40406

check your no. of column and its name cursor.getColumnCount() and cursor.getColumnName(0). respectively.if your column count is 2 then cursor have two column

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

after completion of getting data from database close your cursor using cursor.close();

Upvotes: 1

papaiatis
papaiatis

Reputation: 4291

// 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!

Upvotes: 0

Related Questions