SQLite資料庫的增刪改查

2022-08-15 09:57:20 字數 1837 閱讀 5585

1、資料庫的curd

1

public

class

persondao

1213

//增刪改查

1415

/**16

* 往資料庫新增一條資料

17*/

18public

void

add(string name, string phone) );

27//

關閉資料庫 釋放資料庫的鏈結

28db.close();29}

30}3132

/**33

* 查詢資料庫的操作

34*/

35public

boolean

find(string name) );

41if

(cursor.movetofirst())

48//

記得關閉掉 cursor

49cursor.close();

50 result = false;51

//釋放資料庫的鏈結

52db.close();53}

54return

result;55}

5657

/**58

* 刪除一條記錄

59*

60*

@param

name

61*/

62public

void

delete(string name) );

67db.close();68}

69}7071

/**72

* 更新一條記錄

73*

74*/

75public

void

update(string name, string newname, string newphone) );

80db.close();81}

82}8384

/**85

* 查詢全部

86*/

87public listgetallpersons()

103cursor.close();

104db.close();

105}

106return

persons;

107}

108109 }

2、用junit測試

1

public

class testpersondao extends

androidtestcase

*/14

public

void testadd() throws

exception19}

20public

void testdelete() throws

exception

24public

void testupdate() throws

exception

2829

public

void testfindall() throws

exception

34 }

使用junit步驟:

1 adndroid-junit

2 1、androidmanifest.xml新增

34   android:name="android.test.instrumentationtestrunner"

5   android:targetpackage="cn.zengfansheng.db"

6 />

8

SQLite資料庫的增刪改查

1 資料庫的curd 1 public class persondao 1213 增刪改查 1415 16 往資料庫新增一條資料 17 18public void add string name,string phone 27 關閉資料庫 釋放資料庫的鏈結 28db.close 29 30 3132...

SQLite資料庫建立 增刪改查

建立資料庫 public class myopenhelpter extends sqliteopenhelper 呼叫時機 第一次建立資料庫時,oncreate會執行 override public void oncreate sqlitedatabase db override public v...

fmdb實現sqlite資料庫的增刪改查功能 上

移動端開發中許多地方需要用到一些資料持久化操作,比如將一些資料直接寫入沙盒,存入偏好設定,歸檔等。但是有些時候需要把一些資料量稍微大一點的東西做本地的儲存,比如用第三方環信做im開發中我們需要將使用者資訊儲存到移動端,所以就使用到了sqlite,一款在移動開發中經常用到的嵌入式資料庫。如果我們使用原...