ローカル db(sqlite) に日付を挿入します。しかし、このデータを選択したい場合、カーソルは xml を構築せず、このブロックをスキップします。sqlite に 1 行あります。
これは私のコードです:`
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;
}`