我在我的本地db(sqlite)中插入一些日期。但是当我想选择这个数据时,游标不是构建xml并跳过这个块。我的sqlite中有一行。你有什么想法吗?
这是我的代码:`
public void insertMessages(int msgid,String msgdesc, String date, int createdby,int to) {
SQLiteDatabase myDatabase = this.getWritableDatabase();
String query = "INSERT INTO Messages (_id,msgdesc,date,createdby,sendto) "+
" VALUES ("+msgid+",'"+msgdesc+"','"+date+"',"+createdby+","+to+")";
SQLiteStatement st = myDatabase.compileStatement(query);
try {
st.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myDatabase.close();
Log.i("insert","insert Messages completed");
}
public String getMessages(int to) {
SQLiteDatabase myDatabase = this.getWritableDatabase();
String xml = null;
try {
String query = "SELECT _id,msgdesc,date,createdby,sendto from Messages Where sendto="+to+"";
Cursor cs = myDatabase.rawQuery(query, null);
xml = "<messagelist>";
*****if (cs.moveToFirst()) {*****
do {
xml += "<messages>";
xml += "<msgid>"+cs.getInt(cs.getColumnIndex("_id"))+"</msgid>";
xml += "<msgdesc>"+cs.getString(cs.getColumnIndex("msgdesc"))+"</msgdesc>";
xml += "<date>"+cs.getString(cs.getColumnIndex("date"))+"</date>";
xml += "<createdby>"+cs.getInt(cs.getColumnIndex("createdby"))+"</createdby>";
xml += "<to>"+cs.getInt(cs.getColumnIndex("sendto"))+"</to>";
xml += "</messages>";
} while (cs.moveToNext());
}
xml += "</messagelist>";
cs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myDatabase.close();
Log.i("select","select completed");
return xml;
}`
答案 0 :(得分:1)
"SELECT _id,msgdesc,date,createdby,sendto from Messages Where sendto="+to+"";
您必须使用单引号分隔字符串常量。试试这个
"SELECT _id,msgdesc,date,createdby,sendto from Messages Where sendto = '" + to + "'";
更好的方法是使用参数化查询。