Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中按名称搜索记录_Android_Sqlite - Fatal编程技术网

在android中按名称搜索记录

在android中按名称搜索记录,android,sqlite,Android,Sqlite,我用sq-lite创建了这个表 String TABLE_CREATE_QUERY = "create table contacts(id integer primary key autoincrement,name text not null, fname text not null, regid integer unique not null,year text not null, email text not null, phone text not null,

我用sq-lite创建了这个表

String TABLE_CREATE_QUERY = 
            "create table contacts(id integer primary key autoincrement,name text not null, fname text not null, regid integer unique not null,year text not null, email text not null, phone text not null, address text not null)";
还有一种按名称搜索记录的方法

public Cursor searchByName(String name)
    {

        return db.query(DATABASE_TABLE, new String[] {"id","name", "phone"},
"name="+name, null, null, null, null);
    }
现在,当我想像这样搜索uzair的记录时,我得到了一个错误

open(); //open database;
 Cursor c=searchByName("uzair");
close();  //close database.

任何帮助都将不胜感激……

您没有将SQL字符串用引号括起来:
“name=”+name+“”

但实际上,您应该参数化查询,以保护数据库免受注入攻击:

return db.query(DATABASE_TABLE, new String[] {"id","name", "phone"}, 
        "name=?", new String[] {name}, null, null, null);
(如果这不是完整的解决方案,您需要发布您的LogCat错误,这样我们就不必猜测。)


添加
您添加了以下评论:

if(student_name.length()>0){open();Cursor c=searchByName(student_name);c.moveToFirst();name=newstring[c.getCount()];for(int i=0;iif(student_name.length()>0){open();Cursor c=searchByName(student_name);c.moveToFirst();name=newstring[c.getCount()];for(int i=0;i=0;i=0
for(int i=0;i<names.length;i++) { 
    names[i]=c.getString(1); 
    c.moveToNext();
}