# html5-db
**Repository Path**: i5ting/html5-db
## Basic Information
- **Project Name**: html5-db
- **Description**: No description available
- **Primary Language**: JavaScript
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2014-03-13
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# html5-db
浏览器端数据库用法
## 支持的db
支持2种db
- WebSQL DB
是关系型数据库(rdbms)
- IndexedDB
是NoSQL
## WebSQL DB
### 场景
- 用户表user
用户名,密码
- 任务task
任务标题,任务描述,是否已完成
- 任务项task_item
内容,是否完成
- 回执task_receipt
任务id,完成时间,用户id
**关系**
- 用户和任务多对多
- 一个任务有多个任务项
## 迭代1
- 创建用户表
- 用户注册逻辑
- 完成登陆逻辑
- 用户列表
- 删除某个用户
- 清空所有用户
### 创建一个数据库
var myDB = window.openDatabase('myData','1.0','测试数据库',102400);//数据库名,版本号,描述,大小
### 创建user表
//开启SQLite数据库事务
myDB.transaction(function(tx){
//执行一个SQL语句,创建一个数据库表,表名为user,表字段id为浮点数类型,name为文本类型,password为文本类型
tx.executeSql("create table if not exists user (id real PRIMARY KEY,name text unique,password text)",[]);
});
### 注册
$('#btn01').on('click',function(){
var name = $('#text01').val();
var password = $('#text02').val();
myDB.transaction(function(tx){
//执行sql语句查询注册的用户名是否存在
tx.executeSql("select * from user where name='"+name+"'and password='"+password+"'",[],function(tx, result) {
//如果存在
if(result.rows.length > 0){
alert('用户名已存在');
}else{
//如果不存在就执行sql语句插入到表中
tx.executeSql("insert into user (id,name,password) values(?,?,?)",[user_id_count,name,password],function(tx, result) {
alert('注册成功');
},function(tx,error){
alert('注册失败');
});
}
})
})
})
### 登录
$('#btn02').on('click',function(){
var name = $('#text03').val();
var password = $('#text04').val();
myDB.transaction(function(tx){
//执行sql语句查询登录的用户名是否存在表中
tx.executeSql("select * from user where name='"+name+"'and password='"+password+"'",[],function(tx, result) {
if(result.rows.length>0){
alert('登录成功');
}else{
alert('登录失败');
}
},function(error){
alert('登录失败');
})
})
})
### 用户列表
myDB.transaction(function(tx){
tx.executeSql("select * from user",[],function(tx,result){
for(var i=0;i" + result.rows.item(i).name + ""+ result.rows.item(i).password +"删除";
console.log(result.rows.item(i));
$('.list').append(lis);
}
})
});
### 删除一个用户
$('.list').on('click',function(event){
var del = event.target;
if($(del).hasClass('del')){
var uid = parseInt($(del).attr('uid'));
myDB.transaction(function(tx){
//删除用户表中id等于当前点击的用户
tx.executeSql("delete from user where id = " + uid,[],function(tx,result){
alert('删除成功');
})
})
}
})
### 清空表
$('#btn03').on('click',function(){
myDB.transaction(function(tx){
tx.executeSql("delete from user",[],function(tx,result){
alert('删除成功');
})
})
});
### 删除表
$('#btn04').on('click',function(){
myDB.transaction(function(tx){
tx.executeSql("drop table user",[],function(tx,result){
alert('删除成功');
})
})
});
## 迭代2
创建任务和任务项,查询出一个任务的详细内容
### 数组里的常用方法
- push
- pop
- shift
- unshift
### 递归写法
```
//插入任务项
$('#taskAdd').on('click',function(){
var taskItem = $('.divinclude input');
//准备数据,把数据放在数组里
var taskItems = [];
for(var i=0;i y
var r4 = IDBKeyRange.lowerBound(y, true);
// All keys ≥ x && ≤ y
var r5 = IDBKeyRange.bound(x, y);
// All keys > x &&< y
var r6 = IDBKeyRange.bound(x, y, true, true);
// All keys > x && ≤ y
var r7 = IDBKeyRange.bound(x, y, true, false);
// All keys ≥ x &&< y
var r8 = IDBKeyRange.bound(x, y, false, true);
// The key = z
var r9 = IDBKeyRange.only(z);
前三个方法(lowerBound、upperBound和bound)默认包括端点值,可以传入一个布尔值,修改这个属性。
生成Range对象以后,将它作为参数输入openCursor方法,就可以在所设定的范围内读取数据。
var t = db.transaction(["people"],"readonly");
var store = t.objectStore("people");
var index = store.index("name");
var range = IDBKeyRange.bound(start, end);
index.openCursor(range).onsuccess = function(e) {
var cursor = e.target.result;
if(cursor) {
console.log(cursor.key + ":");
for(var field in cursor.value) {
console.log(cursor.value[field]);
}
cursor.continue();
}
}
## 参考文章
- [Migrating your WebSQL DB to IndexedDB](http://www.html5rocks.com/en/tutorials/webdatabase/websql-indexeddb/?redirect_from_locale=zh)
- [IndexedDB:浏览器端数据库](http://javascript.ruanyifeng.com/bom/indexeddb.html)
- Raymond Camden, [Working With IndexedDB – Part 1](http://net.tutsplus.com/tutorials/javascript-ajax/working-with-indexeddb/)
- Raymond Camden, [Working With IndexedDB – Part 2](http://net.tutsplus.com/tutorials/javascript-ajax/working-with-indexeddb-part-2/)