如何调用此方法将字符串作为参数或如何执行此查询?
代码:
public class Samsung extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_samsung);
Intent intent = getIntent();
ArrayList<HashMap<String, String>> SamsungList = getSamsungInfo(null);
ListAdapter adapter = new SimpleAdapter(Samsung.this, SamsungList, R.layout.samsung_list, new String[] {Sqlite.COLUMN_MNAME, Sqlite.COLUMN_MMODEL,
Sqlite.COLUMN_MPRICE,Sqlite.COLUMN_MCAMERA} , new int[] {R.id.nameofsamsung, R.id.modelofsamsung, R.id.priceofsamsung, R.id.cameraofsamsung});
setListAdapter(adapter);
}
public ArrayList <HashMap<String, String>> getSamsungInfo(String samsung) {
ArrayList <HashMap<String, String>> wordList;
wordList= new ArrayList <HashMap<String, String>>();
Sqlite sqlll = new Sqlite(this);
SQLiteDatabase database = sqlll.getReadableDatabase();
String selectQuery = "SELECT * FROM mobile WHERE mname="+samsung+"'";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put(Sqlite.COLUMN_MNAME, cursor.getString(0));
map.put(Sqlite.COLUMN_MMODEL, cursor.getString(1));
map.put(Sqlite.COLUMN_MPRICE, cursor.getString(2));
map.put(Sqlite.COLUMN_MCAMERA, cursor.getString(3));
wordList. add(map);
} while (cursor.moveToNext());
}
return wordList ;
}
}
给我一个错误,就是没有名为&#34; null&#34;的列。那么在调用方法时我应该通过什么呢。它没有采取三星,它是移动列mname中的插入值。
答案 0 :(得分:4)
您需要在SQL中引用您的文字:
String selectQuery = "SELECT * FROM mobile WHERE mname='"+samsung+"'";
虽然最好使用bind args,如下所示:
String selectQuery = "SELECT * FROM mobile WHERE mname=?";
Cursor cursor = database.rawQuery(selectQuery, new String[] { samsung });
另外,将实际参数传递给getSamsungInfo()
而不是null
。
答案 1 :(得分:1)
语法错误:
String selectQuery = "SELECT * FROM mobile WHERE mname="+samsung+"'";
它应该是(正确的撇号来包围字符串值):
String selectQuery = "SELECT * FROM mobile WHERE mname = 'samsung'";
OR,更好(这里我使用变量),
String selectQuery = "SELECT * FROM mobile WHERE mname = '" + mobileName + "'";
其中mobileName是一个传递的字符串,其中包含(例如)&#34; samsung&#34;
OR,甚至更好(绑定参数),
String selectQuery = "SELECT * FROM mobile WHERE mname = ?";
接下来是:
Cursor cursor = database.rawQuery(selectQuery, new String[] {mobileName});
其中mobileName是一个传递的字符串,其中包含(例如)&#34; samsung&#34; - 与之前相同
这里null
被字符串参数