SQLite資料庫的簡單讀寫操作

2021-08-21 04:27:06 字數 1171 閱讀 4910

安卓系統自帶sqlite資料庫,sdk中對sqlite的操作由sqlitedatabase完成,涉及到的類有如下幾個:

1、sqlitedatabase:代表資料庫本身,支援對資料的標準sql操作

2、cursor:用來實現對查詢結果集的隨機讀寫

下面**實現如何開啟資料庫,並建立資料表:

sqlitedatabase db;

db = openorcreatedatabase(db_name, this.mode_private, null); 

db.execsql("drop table if exists students");    

db.execsql("create table if not exists students (_id integer primary key autoincrement, name varchar, age integer)"); 

db.execsql("insert into students values (null,?,?)",new object);

實際上,db.execsql()函式就可以執行全部的sql指令。

下面的**實現如何通過cursor迴圈讀取資料表中的資料:

cursor c = db.rawquery("select * from students",null); 

while (c.movetonext())

c.close();

db.close();

cursor的另一些操作

通過getcount()方法得到結果集中有多少記錄;

通過getcolumnnames()得到欄位名;

通過getcolumnindex()轉換成欄位號;

通過getstring().getint()等方法得到給定字段當前記錄的值;

通過requery()方法重新還行查詢得到游標;

通過close()方法釋放游標資源。

cursor的移動操作:

movetonext()

movetoprevious()

movetolast()

movetofirst()

move(int offset)從當前位置向上或向下移動的行數,offset的正負分別代表向下和向上;

movetoposition(int position)移到position指定的行

SQLite 簡單的資料庫

1.建立資料庫和表 引數1.資料儲存的檔案位置 引數2.檔案建立工廠類,這裡不需要,寫為空 db sqlitedatabase.openorcreatedatabase data data com.coderqi.android2 lesson 04 database database.db nul...

android筆記之SQLite 資料庫操作

資料庫的初始化 public class blacklistdb extends sqliteopenhelper override public void oncreate sqlitedatabase sqlitedatabase override public void onupgrade s...

Python基礎 SQLite資料庫基本操作

sqlite資料庫的官網 有很多管理資料庫的工具,官方使用命令列進行管理,感覺太麻煩,使用db browser進行管理資料庫,附上官網 開啟後還是中文的,驚喜 安裝庫 pysqlite3 在進行對資料庫的操作之前,首先要使用函式connect開啟資料庫 通過物件的execute方法進行資料庫的各種操...