資料庫操作總結

來源:互聯網
上載者:User

SQLiteDatabase 是關於資料庫操作的 可用於 insert delete update query 等操作 可惜美中不足的是:

1. 不支援建立資料庫

2. 不支援版本更新

所以出現了SQLiteOpenHelper類,是一個抽象類別,一般繼承它

一般裡面要寫3個函數:

建構函式

oncreate()初始化的時候使用,一般用於建表

onupdate()升級時候使用,一般刪除現有表,然後在重建

然後使用SQLiteOpenHelper的類函數:getWritableDatabase()或者getReadableDatabase()擷取資料庫執行個體

然後進行query,insert,update,delete操作。

 mDb.insert(TABLE_NAME, null, newValues);

newValues是一個類似bundle和hashtable的集合,儲存索引值對

mDb.delete(TABLE_NAME, ID + "=" + id, null);

return mDb.delete(TABLE_NAME, null, null);

public PersonInfo[] queryOneData(long id)

 {

  Cursor result = mDb.query(TABLE_NAME, new String[] { ID,
    PERSONINFO_CODE, PERSONINFO_DISPLAYPICTURE,
    PERSONINFO_NICKNAME, PERSONINFO_BIRTHDAY, PERSONINFO_GENDER,
    PERSONINFO_SIGNATURE, PERSONINFO_SIGNATUREDATETIME,
    PERSONINFO_PASSWORD, PERSONINFO_DISPLAYPICTUREBINARY,
    PERSONINFO_REGISTERTIME, PERSONINFO_UPDATETIME },

  ID + "=" + id, null, null, null, null);

  PersonInfo[]pInfo=ConvertToPersonInfo(result);
  if(result!=null&&!result.isClosed()){
   result.close();
   result=null;
  }
  return pInfo;

 }

 

public long updateOneData(long id, PersonInfo personInfo)

 {

  ContentValues newValues = new ContentValues();

  newValues.put(PERSONINFO_CODE, personInfo.getCode());

  newValues
    .put(PERSONINFO_DISPLAYPICTURE, personInfo.getDisplayPicture());

  newValues.put(PERSONINFO_NICKNAME, personInfo.getNickname());

  newValues.put(PERSONINFO_BIRTHDAY, personInfo.getBirthday());

  newValues.put(PERSONINFO_GENDER, personInfo.getGender());

  newValues.put(PERSONINFO_SIGNATURE, personInfo.getSignature());

  newValues.put(PERSONINFO_SIGNATUREDATETIME, personInfo
    .getSignatureDatatime());

  newValues.put(PERSONINFO_PASSWORD, personInfo.getPassword());

  newValues.put(PERSONINFO_DISPLAYPICTUREBINARY, "");

  newValues.put(PERSONINFO_REGISTERTIME, personInfo.getRegisterTime());

  newValues.put(PERSONINFO_UPDATETIME, personInfo.getUpdateTime());

  return mDb.update(TABLE_NAME, newValues, ID + "=" + id, null);

 }

 

public long insert(PersonInfo personInfo)

 {

  ContentValues newValues = new ContentValues();

  newValues.put(PERSONINFO_CODE, personInfo.getCode());

  newValues
    .put(PERSONINFO_DISPLAYPICTURE, personInfo.getDisplayPicture());

  newValues.put(PERSONINFO_NICKNAME, personInfo.getNickname());

  newValues.put(PERSONINFO_BIRTHDAY, personInfo.getBirthday());

  newValues.put(PERSONINFO_GENDER, personInfo.getGender());

  newValues.put(PERSONINFO_SIGNATURE, personInfo.getSignature());

  newValues.put(PERSONINFO_SIGNATUREDATETIME, personInfo
    .getSignatureDatatime());

  newValues.put(PERSONINFO_PASSWORD, personInfo.getPassword());

  newValues.put(PERSONINFO_DISPLAYPICTUREBINARY, "");

  newValues.put(PERSONINFO_REGISTERTIME, personInfo.getRegisterTime());

  newValues.put(PERSONINFO_UPDATETIME, personInfo.getUpdateTime());

  return mDb.insert(TABLE_NAME, null, newValues);

 }

public long deleteOneData(long id)

 {

  return mDb.delete(TABLE_NAME, ID + "=" + id, null);

 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.