溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

html本地?cái)?shù)據(jù)庫(kù)實(shí)例分析

發(fā)布時(shí)間:2022-03-09 10:57:24 來源:億速云 閱讀:318 作者:iii 欄目:web開發(fā)

今天小編給大家分享一下html本地?cái)?shù)據(jù)庫(kù)實(shí)例分析的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

下面將一一將介紹怎樣創(chuàng)建打開數(shù)據(jù)庫(kù),創(chuàng)建表,添加數(shù)據(jù),更新數(shù)據(jù),刪除數(shù)據(jù),刪除表 。

先介紹三個(gè)核心方法

1、openDatabase:這個(gè)方法使用現(xiàn)有數(shù)據(jù)庫(kù)或創(chuàng)建新數(shù)據(jù)庫(kù)創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象。

2、transaction:這個(gè)方法允許我們根據(jù)情況控制事務(wù)提交或回滾。

3、executeSql:這個(gè)方法用于執(zhí)行真實(shí)的SQL查詢。

第一步:打開連接并創(chuàng)建數(shù)據(jù)庫(kù)

復(fù)制代碼 代碼如下:

var dataBase = openDatabase("student", "1.0", "學(xué)生表", 1024 * 1024, function () { });

if (!dataBase) {

alert("數(shù)據(jù)庫(kù)創(chuàng)建失??!");

} else {

alert("數(shù)據(jù)庫(kù)創(chuàng)建成功!");

}

解釋一下openDatabase方法打開一個(gè)已經(jīng)存在的數(shù)據(jù)庫(kù),如果數(shù)據(jù)庫(kù)不存在,它還可以創(chuàng)建數(shù)據(jù)庫(kù)。幾個(gè)參數(shù)意義分別是:

1,數(shù)據(jù)庫(kù)名稱。

2,版本號(hào) 目前為1.0,不管他,寫死就OK。

3,對(duì)數(shù)據(jù)庫(kù)的描述。

4,設(shè)置數(shù)據(jù)的大小。

5,回調(diào)函數(shù)(可省略)。

初次調(diào)用時(shí)創(chuàng)建數(shù)據(jù)庫(kù),以后就是建立連接了。

創(chuàng)建的數(shù)據(jù)庫(kù)就存在本地,路徑如下:

C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default\databases\http_localhost_4987 。

創(chuàng)建的是一個(gè)sqllite數(shù)據(jù)庫(kù),可以用SQLiteSpy打開文件,可以看到里面的數(shù)據(jù)。SQLiteSpy是一個(gè)綠色軟件,可以百度一下下載地址或SQLiteSpy官方下載:SQLiteSpy。

第二步:創(chuàng)建數(shù)據(jù)表

復(fù)制代碼 代碼如下:

this.createTable=function() {

dataBase.transaction( function(tx) {

tx.executeSql(

"create table if not exists stu (id REAL UNIQUE, name TEXT)",

[],

function(tx,result){ alert('創(chuàng)建stu表成功'); },

function(tx, error){ alert('創(chuàng)建stu表失敗:' + error.message);

});

});

}

解釋一下,

executeSql函數(shù)有四個(gè)參數(shù),其意義分別是:

1)表示查詢的字符串,使用的SQL語(yǔ)言是SQLite 3.6.19。

2)插入到查詢中問號(hào)所在處的字符串?dāng)?shù)據(jù)。

3)成功時(shí)執(zhí)行的回調(diào)函數(shù)。返回兩個(gè)參數(shù):tx和執(zhí)行的結(jié)果。

4)一個(gè)失敗時(shí)執(zhí)行的回調(diào)函數(shù)。返回兩個(gè)參數(shù):tx和失敗的錯(cuò)誤信息。

第三步:執(zhí)行增刪改查

1)添加數(shù)據(jù):

復(fù)制代碼 代碼如下:

this.insert = function () {

dataBase.transaction(function (tx) {

tx.executeSql(

"insert into stu (id, name) values(?, ?)",

[id, '徐明祥'],

function () { alert('添加數(shù)據(jù)成功'); },

function (tx, error) { alert('添加數(shù)據(jù)失敗: ' + error.message);

} );

});

2)查詢數(shù)據(jù)

復(fù)制代碼 代碼如下:

this.query = function () {

dataBase.transaction(function (tx) {

tx.executeSql(

"select * from stu", [],

function (tx, result) { //執(zhí)行成功的回調(diào)函數(shù)

//在這里對(duì)result 做你想要做的事情吧...........

},

function (tx, error) {

alert('查詢失敗: ' + error.message);

} );

});

}

解釋一下

上面代碼中執(zhí)行成功的回調(diào)函數(shù)有一參數(shù)result。

result:查詢出來的數(shù)據(jù)集。其數(shù)據(jù)類型為 SQLResultSet ,就如同C#中的DataTable。

SQLResultSet 的定義為:

復(fù)制代碼 代碼如下:

interface SQLResultSet {

readonly attribute long insertId;

readonly attribute long rowsAffected;

readonly attribute SQLResultSetRowList rows;

};

其中最重要的屬性—SQLResultSetRowList 類型的 rows 是數(shù)據(jù)集的“行” 。

rows 有兩個(gè)屬性:length、item 。

故,獲取查詢結(jié)果的某一行某一列的值 :result.rows[i].item[fieldname]  。

3)更新數(shù)據(jù)

復(fù)制代碼 代碼如下:

this.update = function (id, name) {

dataBase.transaction(function (tx) {

tx.executeSql(

"update stu set name = ? where id= ?",

[name, id],

function (tx, result) {

},

function (tx, error) {

alert('更新失敗: ' + error.message);

});

});

}

4)刪除數(shù)據(jù)

復(fù)制代碼 代碼如下:

this.del = function (id) {

dataBase.transaction(function (tx) {

tx.executeSql(

"delete from stu where id= ?",

[id],

function (tx, result) {

},

function (tx, error) {

alert('刪除失敗: ' + error.message);

});

});

}

5)刪除數(shù)據(jù)表

復(fù)制代碼 代碼如下:

this.dropTable = function () {

dataBase.transaction(function (tx) {

tx.executeSql('drop table stu');

});

}

以上就是“html本地?cái)?shù)據(jù)庫(kù)實(shí)例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI