从数据库中获得源码信息数据库中、源码、信息

2023-09-05 10:29:03 作者:小时光

如何从数据库中获得的信息。我想执行此行

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 person

推荐答案

请试试这个,如果你的id是整数数据类型。

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

皮斯试试这个,如果你的id是字符串数据类型。

 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; 
}
 
精彩推荐
图片推荐