开发者

get information from database sqlite

开发者 https://www.devze.com 2023-02-21 17:23 出处:网络
how to get an information from the database. I want to execute this line String nom = db.execSQL(\"select name from person where id=\'+id+\'\");

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

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

Can any one correct me this line to get the name of person from the table 开发者_JS百科person


Please try this if your id is integer datatype.

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 String datatype.

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


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 the idea).

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

execSQL() documentation:

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号