Get information from sqlite database

how to get information from the database. I want to execute this line

String nom = db.execSQL("select name from person where id='+id+'");

Can someone correct me this line to get the name of the person from the person of the table

+3
source share
2 answers

Try this if your identifier is an integer data type.

public String getResult(int id)
{
    String name = null;
    try
    {
        Cursor c = null;
        c = db.rawQuery("select name from person where id="+id, null);
        c.moveToFirst();
        name = c.getString(c.getColumnIndex("name"));
        c.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return name; 
}

Pease try this if your id is a String data type.

 public String getResult(String id)
{
    String name = null;
    try
    {
        Cursor c = null;
        c = db.rawQuery("select name from person where id=" + "\""+id+"\"", null);
        c.moveToFirst();
        name = c.getString(c.getColumnIndex("name"));
        c.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return name; 
}
+8
source
Cursor cursor = db.rawQuery("SELECT name FROM person WHERE id = ?", new String[] { id });
cursor.moveToFirst();
String nom = cursor.getString(cursor.getColumnIndex("name"));

this should be the easiest way (not tested, but you should get an idea).

It also looks like you have no idea how the android handles access to the database, so I recommend looking at least at the Cursor class .

execSQL() documentation:

SQL, SELECT - SQL, .

+1

Source: https://habr.com/ru/post/1529984/


All Articles