I have taken String
value from a EditText
and set it inside SELECT QUERY after WHERE
condition
As
TextView tv = (TextView) findViewById(R.id.textView3); EditTextet2 et = (EditText) findViewById(R.id.editText1); String name = et.getText().toString(); Cursor c = db.rawQuery("SELECT * FROM tbl1 WHERE name = '"+name+"'", null); c.moveToNext(); tv.setText(c.getString(c.getColumnIndex("email")));
But it doesn't work. Any suggestions?
The Android SDK includes a sqlite3 shell tool that allows you to browse table contents, run SQL commands, and perform other useful functions on SQLite databases.
We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data. This method return the total number of columns of the table.
Cursors are what contain the result set of a query made against a database in Android. The Cursor class has an API that allows an app to read (in a type-safe manner) the columns that were returned from the query as well as iterate over the rows of the result set.
SQLite Database is an open-source database provided in Android which is used to store data inside the user's device in the form of a Text file. We can perform so many operations on this data such as adding new data, updating, reading, and deleting this data.
Try trimming the string to make sure there is no extra white space:
Cursor c = db.rawQuery("SELECT * FROM tbl1 WHERE TRIM(name) = '"+name.trim()+"'", null);
Also use c.moveToFirst()
like @thinksteep mentioned.
This is a complete code for select statements.
SQLiteDatabase db = this.getReadableDatabase(); Cursor c = db.rawQuery("SELECT column1,column2,column3 FROM table ", null); if (c.moveToFirst()){ do { // Passing values String column1 = c.getString(0); String column2 = c.getString(1); String column3 = c.getString(2); // Do something Here with values } while(c.moveToNext()); } c.close(); db.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With