仿微信 介面動畫 6 資料庫的簡易操作

2021-07-16 23:21:03 字數 4227 閱讀 1667

下面簡述了一下如何建立資料庫,如何增刪改查,以及資料庫的新增列的操作

1.簡歷資料庫,首先建立乙個資料庫的輔助類mydatabasehelper繼承的是sqliteopenhelper

//資料庫操作的輔助類

public

class

mydatabasehelper

extends

sqliteopenhelper

@override

public

void

oncreate(sqlitedatabase db)

//只有

public

void

onupgrade(sqlitedatabase db, int oldversion, int newversion)

}}

2.資料庫的增刪改查以及版本的更新

(1)建立

mydatabasehelper mydatabasehelper= new mydatabasehelper(this, "test.db", null, 1);

mydatabasehelper.getwritabledatabase();

若是已經建立了名字叫test.db的資料庫,則不會再呼叫oncreate方法(mydatabasehelper

中);否則,會呼叫一(oncreate方法)

而oncreate方法中是執行一sq語句,建立**user

(2)增加

sqlitedatabase database = mydatabasehelper.getwritabledatabase();// 開啟資料庫的連線

contentvalues values = new contentvalues();// 和hash表類似,儲存資料,鍵值對形式

"userid", 1);

values.put("username", "tom");

database.insert("user", null, values);

"userid", 2);

values.put("username", "jerry");

database.insert("user", null, values);

(3)刪除

sqlitedatabase database2 = mydatabasehelper.getwritabledatabase();// 開啟資料庫的連線

// 事務(若是一句話執行錯誤的話,出錯後前面的操作hi被撤銷,這叫做事務的回滾)

database2.begintransaction();// 事務的開始

try );

database2.settransactionsuccessful();// 提交到資料庫

} catch (exception e) finally

(4)改

sqlitedatabase database1 = mydatabasehelper.getwritabledatabase();// 開啟資料庫的連線

// 借助contentvalues

// id是1的使用者改名字

contentvalues values1 = new contentvalues();// 和hash表類似,儲存資料,鍵值對形式

values1.put("username", "chenle");

database1.update("user", values1, "userid = ?",

newstring );

(5)查

// cursor游標指向的是一行資料報含id資料,包含name資料,包含age資料

sqlitedatabase database3 = mydatabasehelper.getwritabledatabase();// 開啟資料庫的連線

cursor cursor = database3.query("user", new

string , "userid =?", new

string , null,

null, null);

while (cursor.movetonext())

(6)更新

mydatabasehelper = new mydatabasehelper(this, "test.db", null, 2);

//只有版本變了的時候,才會呼叫onupgrade方法

mydatabasehelper.getwritabledatabase();

跟新的時候其實只是開啟的時候版本不一樣,就會呼叫資料庫輔助類中的onupgrade方法

3.下面講述了的根據使用者姓名和密碼的登入介面的匹配以及註冊使用者的**片段

(1)註冊使用者

mydatabasehelper mydatabasehelper = new mydatabasehelper(this, "test.db",

null, 2);

sqlitedatabase database = mydatabasehelper.getwritabledatabase();// 開啟資料庫的連線

contentvalues values = new contentvalues();// 和hash表類似,儲存資料,鍵值對形式

values.put("username", edittext1_zhanghu.gettext().tostring());

values.put("password", edittext2_password.gettext().tostring());

database.insert("user", null, values);

// 點了註冊之後帳號密碼清空,但是不需要這樣

// edittext1_zhanghu.settext("");

// edittext2_password.settext("");

// 或者這樣註冊(一次性註冊,用sql語句進行)

// sqlitedatabase sdb=dbhelper.getreadabledatabase();

// string

// sql="insert into user(username,password,age,***) values(?,?,?,?)"

; // object

// obj=;

// sdb.execsql(sql, obj);

(2)登入檢測

// sqlitedatabase sdb=dbhelper.getreadabledatabase();

// string sql="select * from user where username=? and password=?"

; // cursor cursor=sdb.rawquery(sql, new string);

// if(cursor.movetofirst()==true)

/** 查詢出來的cursor的初始位置是指向第一條記錄的前乙個位置的 cursor.movetofirst()指向查詢結果的第乙個位置。

* 一般通過判斷cursor

* .movetofirst()的值為true或false來確定查詢結果是否為空。cursor.movetonext

* ()是用來做迴圈的,一般這樣來用:while(cursor.movetonext())

* cursor.movetoprevious()是指向當前記錄的上乙個記錄,是和movetonext相對應的;

* cursor.movetolast()指向查詢結果的最後一條記錄 使用cursor可以很方便的處理查詢結果以便得到想要的資料

*/database = mydatabasehelper.getwritabledatabase();// 開啟資料庫的連線

string sql = "select * from user where username=? and password=?"

; cursor cursor = database.rawquery(sql, new string );

if (cursor.movetofirst() == true) else

希望對你們有所幫助~

微信小程式資料庫的使用

資料庫 1.獲取資料庫引用 const db wx.cloud.database 2.構造查詢語句 collection 方法獲取乙個集合的引用 where 方法傳入乙個物件,資料庫返回集合中欄位等於指定值的 json 文件。api 也支援高階的查詢條件 比如大於 小於 in 等 具體見文件檢視支援...

資料庫 總結5 第6節 資料庫表中記錄的基本操作

dml 增刪改表中資料 1.新增資料 語法 insert into 表名 列名1 列名2,列名n values 值1,值2,值n 注意 1.列名和值要一一對應。2.如果表名後,不定義列名,則預設給所有列新增值。有4個列,你就必須填4個列,少填多填都會報錯。insert into 表名 values ...

微信 WCDB 正式開源,高效易用的移動資料庫框架

wcdb 是乙個高效 完整 易用的移動資料庫框架,基於 sqlcipher,支援 ios macos 和 android。wcdb for ios macos 基本功能 winq wcdb語言整合查詢 通過winq,開發者無須為了拼接sql的字串而寫一大坨膠水 多執行緒高併發 wcdb支援多執行緒讀...