openDatabase–>ts语法
@Injectable({
providedIn: 'root'
})
export class DatabaseService {
win: any = window;
dataSource: any;
constructor(
) {
this.initDB();
}
initDB() {
this.dataSource = this.win.openDatabase('myapp.db', 1, 'windos', 5 * 1024 * 1024);
}
createTable(){
this.dataSource.transaction((tx) => {
tx.executeSql('create table if not exists mydata(id varchar(32), name varchar(5242880))', []);
});
}
insertData(sqlStr: any, sqlParams: any){
this.dataSource.transaction((tx) => {
tx.executeSql(sqlStr, sqlParams,
(tx, result) => {
console.log('插入数据成功!!!');
}, (tx, error) => {
console.error('插入数据成功!!!');
});
});
}
deleteData(sqlStr: any, sqlParams: any){
this.dataSource.transaction((tx) => {
tx.executeSql(sqlStr, sqlParams,
(tx, result) => {
console.log('删除数据成功!!!');
}, (tx, error) => {
console.error('删除数据成功!!!');
});
});
}
updateData(sqlStr: any, sqlParams: any){
this.dataSource.transaction((tx) => {
tx.executeSql(sqlStr, sqlParams,
(tx, result) => {
console.log('修改数据成功!!!');
}, (tx, error) => {
console.error('修改数据成功!!!');
});
});
}
selectData(sqlStr: any, sqlParams: any){
this.dataSource.transaction((tx) => {
tx.executeSql(sqlStr, sqlParams,
(tx, result) => {
console.log('查询数据成功!!!');
}, (tx, error) => {
console.error('查询数据成功!!!');
});
});
}
}
openDatabase–>js语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--
openDatabase与android里面的sqlite差不多
最好的选型存储
-->
<h1>opendatabse数据库操作</h1>
<button id="btn-create">创建user数据表</button>
<button id="btn-insert">插入数据</button>
<button id="btn-query">查询数据</button>
<button id="btn-update">修改数据</button>
<button id="btn-delete">删除数据</button>
<button id="btn-drop">删除user数据表</button>
<script type="text/javascript">
let findId = id => document.getElementById(id);
let user = {
username: "liuqiang",
password: "123569874",
info: "beaconApp开发团队中一员"
};
let db = openDatabase("MySql", "1.0", "我的数据库描述", 1024 * 1024);
let result = db ? "数据库创建成功" : "数据库创建失败";
console.log(result);
const USER_TABLE_SQL = "create table if not exists userTable (id integer primary key autoincrement,username varchar(12)," +
"password varchar(16),info text)";
function createTable() {
db.transaction(tx => {
tx.executeSql(USER_TABLE_SQL, [],
(tx, result) => {
alert('创建user表成功:' + result);
}, (tx, error) => {
alert('创建user表失败:' + error.message);
})
})
}
const INSERT_USER_SQL = "insert into userTable (username, password,info) values(?,?,?)";
function insertData(user) {
db.transaction(tx => {
tx.executeSql(INSERT_USER_SQL,
[user.username, user.password, user.info],
(tx, result) => {
alert('添加数据成功:');
}, (tx, error) => {
alert('添加数据失败:' + error.message);
})
})
}
const QUERY_USER_SQL = "select * from userTable";
function queryData() {
db.transaction(tx => {
tx.executeSql(QUERY_USER_SQL, [],
(tx, result) => {
console.log(result);
},
(tx, error) => {
console.log('查询失败: ' + error.message)
})
})
}
const UPDATE_USER_SQL = "update userTable set password = ? where username = ?";
function updateData(user) {
db.transaction(tx => {
tx.executeSql(UPDATE_USER_SQL, [user.password, user.username],
(tx, result) => {
alert("修改数据成功")
}, (tx, error) => {
alert("修改数据失败:" + error.message)
})
})
}
const DELETE_USER_SQL = "delete from userTable where username = ?";
function deleteData(user) {
db.transaction(tx => {
tx.executeSql(DELETE_USER_SQL, [user.username],
(transaction, resultSet) => {
alert("删除数据成功")
}, (transaction, error) => {
alert("删除数据失败:" + error.message)
})
});
}
const DROP_USER_SQL = "drop table userTable";
function dropTable() {
db.transaction(tx => {
tx.executeSql(DROP_USER_SQL, [],
(transaction, resultSet) => {
alert("删除数据表成功")
}, (transaction, error) => {
alert("删除数据表失败:" + error.message)
})
})
}
let btnCreate = findId("btn-create");
let btnInsert = findId("btn-insert");
let btnQuery = findId("btn-query");
let btnUpdate = findId("btn-update");
let btnDelete = findId("btn-delete");
let btnDrop = findId("btn-drop");
btnCreate.onclick = () => createTable();
btnInsert.onclick = () => insertData(user);
btnQuery.onclick = () => queryData();
btnUpdate.onclick = () => {
user.password = "111666666";
updateData(user);
};
btnDelete.onclick = () => deleteData(user);
btnDrop.onclick = () => dropTable();
</script>
</body>
</html>