[問題] 使用sqlite要做查詢
大大們好~想請教一個問題
小妹最近在寫sqlite(超級新手好QQ
遇到一個問題
這個是我DBHelper.java裡的一個function
Account getAccount(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_ACCOUNT, new String[] { KEY_ID,
KEY_DATE, KEY_TYPE, KEY_MONEY }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Account account = new Account(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), Integer.parseInt(cursor.getString(2)),
Integer.parseInt(cursor.getString(3)));
// return account
return account;
}
最下面的return account是另外一個Account.java
裡面的內容大概長這樣
public class Account {
//private variables
int _id;
String _date;
int _type;
int _money;
// Empty constructor
public Account(){
}
// constructor
public Account(int id, String date, int _type, int _money){
this._id = id;
this._date = date;
this._type = _type;
this._money = _money;
}
// constructor
public Account(String date, int _type, int _money){
this._date = date;
this._type = _type;
this._money = _money;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting date
public String getDate(){
return this._date;
}
// setting date
public void setDate(String date){
this._date = date;
}
// getting type
public int getType(){
return this._type;
}
// setting type
public void setType(int type){
this._type = type;
}
// getting money
public int getMoney(){
return this._money;
}
// setting money
public void setMoney(int money){
this._money = money;
}
}
不外乎就是一些取值跟設定值的function
那如果我現在在MainActivity裡面想要query第一筆的資料要如何寫
我不知道回傳那個account的型態是什麼
我目前寫db.getAccount(0);
可是無法跑出任何東西
不知道要怎模解決
謝謝:)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 163.25.118.248
推
06/16 10:00, , 1F
06/16 10:00, 1F
推
06/16 11:29, , 2F
06/16 11:29, 2F
推
06/16 21:28, , 3F
06/16 21:28, 3F
推
06/16 21:36, , 4F
06/16 21:36, 4F
推
06/16 22:36, , 5F
06/16 22:36, 5F
推
06/17 21:58, , 6F
06/17 21:58, 6F
→
06/17 21:58, , 7F
06/17 21:58, 7F
推
06/19 13:34, , 8F
06/19 13:34, 8F
AndroidDev 近期熱門文章
PTT數位生活區 即時熱門文章