[問題] 使用sqlite要做查詢

看板AndroidDev作者 (YO!!)時間12年前 (2013/06/16 07:55), 編輯推噓7(701)
留言8則, 5人參與, 最新討論串1/1
大大們好~想請教一個問題 小妹最近在寫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
小弟都用ORMLite (汗
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
greenDAO++
06/16 21:36, 4F

06/16 22:36, , 5F
樓上k大是高手
06/16 22:36, 5F

06/17 21:58, , 6F
如果只是讀取,可以考慮小弟寫的ORM
06/17 21:58, 6F

06/17 21:58, , 7F

06/19 13:34, , 8F
話說,你db.query的是 key_id=0,而不是第1筆
06/19 13:34, 8F
文章代碼(AID): #1HlFxzIP (AndroidDev)
文章代碼(AID): #1HlFxzIP (AndroidDev)