2023-07-26 14:31:09 +08:00
|
|
|
|
<template>
|
|
|
|
|
<view class="">
|
|
|
|
|
<!-- <div>{{odb}}</div> -->
|
|
|
|
|
<div>
|
|
|
|
|
<button type="default" @click="isOpenDatabase">判断数据库是否打开</button>
|
|
|
|
|
<button type="default" @click="openDatabase">打开数据库</button>
|
|
|
|
|
<button type="default" @click="closeDatabase">关闭数据库</button>
|
|
|
|
|
<button type="default" @click="createChatTable">创建表</button>
|
|
|
|
|
<button type="default" @click="dropTable">删除表</button>
|
|
|
|
|
<button type="default" @click="transaction">执行事务</button>
|
|
|
|
|
<button type="default" @click="insertChatRow">新增数据</button>
|
|
|
|
|
<button type="default" @click="insertListChatRow">批量新增数据</button>
|
|
|
|
|
<button type="default" @click="deleteChatRow">删除所有数据</button>
|
|
|
|
|
<button type="default" @click="selectSql">查询数据</button>
|
|
|
|
|
</div>
|
|
|
|
|
<div v-for="item in sqlData" :key="item.id">
|
|
|
|
|
<text>{{item.id}}</text>
|
|
|
|
|
<text>{{item.content}}</text>
|
|
|
|
|
</div>
|
|
|
|
|
</view>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
2023-07-28 09:42:53 +08:00
|
|
|
|
import sqlite from "../../common/sqlite.js"
|
2023-07-26 14:31:09 +08:00
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
dbName: 'dianji_chat',
|
|
|
|
|
dbPath: '_doc/dianji_chat.db',
|
|
|
|
|
dbTable: 'dianji_chat',
|
|
|
|
|
dbIsOpen: false,
|
|
|
|
|
sqlData: [],
|
|
|
|
|
chatText: {
|
|
|
|
|
id: 1,
|
|
|
|
|
fromId: '123',
|
|
|
|
|
toId: '321',
|
2023-07-28 09:42:53 +08:00
|
|
|
|
content: 'bihao1',
|
2023-07-26 14:31:09 +08:00
|
|
|
|
flag: 1
|
|
|
|
|
},
|
|
|
|
|
chatText1: [{
|
|
|
|
|
id: 11,
|
|
|
|
|
fromId: '123',
|
|
|
|
|
toId: '321',
|
|
|
|
|
content: '你好!',
|
|
|
|
|
flag: 1
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 12,
|
|
|
|
|
fromId: '123',
|
|
|
|
|
toId: '321',
|
|
|
|
|
content: '你好!',
|
|
|
|
|
flag: 1
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onLoad() {
|
|
|
|
|
// console.log('Sqlite:',Sqlite)
|
|
|
|
|
// this.open();
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
// async open(){
|
|
|
|
|
// let odb=await Sqlite.openSqlite();
|
|
|
|
|
// this.odb=odb;
|
|
|
|
|
// console.log('打开数据库成功:',odb)
|
|
|
|
|
// },
|
|
|
|
|
createChatTable() {
|
|
|
|
|
let sql = 'CREATE TABLE if not exists ' + this.dbTable +
|
|
|
|
|
' ( "id" varchar(32) NOT NULL,"content" varchar(1024),"fromId" varchar(32), "toId" varchar(32), "flag" varchar(2), PRIMARY KEY ("id"));'
|
|
|
|
|
this.executeSql(sql)
|
|
|
|
|
},
|
|
|
|
|
openDatabase() {
|
2023-07-28 09:42:53 +08:00
|
|
|
|
sqlite.openDatabaseCeshi().then((value) => {
|
2023-07-26 15:02:49 +08:00
|
|
|
|
// 在resolve时执行的回调函数
|
|
|
|
|
console.log(value); // 输出:Hello, World!
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
// 在reject时执行的回调函数
|
|
|
|
|
console.error(error);
|
2023-07-26 14:31:09 +08:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
isOpenDatabase() {
|
|
|
|
|
let res = plus.sqlite.isOpenDatabase({
|
|
|
|
|
name: this.dbName,
|
|
|
|
|
path: this.dbPath
|
|
|
|
|
});
|
|
|
|
|
if (res) {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: 'none',
|
|
|
|
|
title: '数据库已打开'
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: 'none',
|
|
|
|
|
title: '数据库未打开'
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
closeDatabase() {
|
|
|
|
|
plus.sqlite.closeDatabase({
|
|
|
|
|
name: this.dbName,
|
|
|
|
|
success: function(e) {
|
|
|
|
|
console.log(JSON.stringify(e), 'closeDatabase dianji_chat success!');
|
|
|
|
|
},
|
|
|
|
|
fail: function(e) {
|
|
|
|
|
console.log('closeDatabase failed: ' + JSON.stringify(e));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
insertChatRow() {
|
|
|
|
|
let data = this.chatText
|
|
|
|
|
data.id++
|
2023-07-28 09:42:53 +08:00
|
|
|
|
// let sql = "insert into " + this.dbTable + " values('" + data.id + "','" + data.content +
|
|
|
|
|
// "','" + data.fromId + "','" + data.toId + "'," + data.flag + ")";
|
2023-08-01 09:32:45 +08:00
|
|
|
|
let sql = "insert into " + this.dbTable + "(id, content, fromId, toId, flag) values('" + data.id + "','" +
|
|
|
|
|
data.content +
|
|
|
|
|
"','" + data.fromId + "','" + data.toId + "'," + data.flag + ")";
|
2023-07-26 14:31:09 +08:00
|
|
|
|
this.executeSql(sql)
|
|
|
|
|
},
|
|
|
|
|
insertListChatRow() {
|
|
|
|
|
let data = this.chatText1
|
|
|
|
|
let sql = '';
|
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
|
|
|
sql = "insert into " + this.dbTable + " values('" + data[i].id + "','" + data[i].content +
|
|
|
|
|
"','" + data[i].fromId + "','" + data[i].toId + "'," + data[i].flag + ")";
|
|
|
|
|
this.executeSql(sql)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
deleteChatRow() {
|
|
|
|
|
let sql = "delete from " + this.dbTable;
|
|
|
|
|
this.executeSql(sql)
|
|
|
|
|
},
|
|
|
|
|
selectSql() {
|
|
|
|
|
let curPage = 1
|
|
|
|
|
let pageSize = 20
|
|
|
|
|
let fromId = '123'
|
2023-07-28 09:42:53 +08:00
|
|
|
|
let sql = 'select * from dianji_chat'
|
|
|
|
|
console.log(111);
|
2023-07-26 14:31:09 +08:00
|
|
|
|
this.executeSql(sql)
|
|
|
|
|
},
|
|
|
|
|
dropTable() {
|
|
|
|
|
let sql = 'DROP TABLE ' + this.dbTable + ';'
|
|
|
|
|
plus.sqlite.executeSql({
|
|
|
|
|
name: this.dbName,
|
|
|
|
|
sql: sql,
|
|
|
|
|
success: function(e) {
|
|
|
|
|
console.log("删除数据表成功");
|
|
|
|
|
},
|
|
|
|
|
fail: function(e) {
|
|
|
|
|
console.log('executeSql failed: ' + JSON.stringify(e));
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
transaction() {
|
|
|
|
|
plus.sqlite.transaction({
|
|
|
|
|
name: this.dbName,
|
|
|
|
|
operation: 'begin',
|
|
|
|
|
success: function(e) {
|
|
|
|
|
console.log('transaction success!');
|
|
|
|
|
},
|
|
|
|
|
fail: function(e) {
|
|
|
|
|
console.log('transaction failed: ' + JSON.stringify(e));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
2023-07-28 09:42:53 +08:00
|
|
|
|
executeSql(sql) {
|
|
|
|
|
sqlite.executeSqlCeshi(sql).then((value) => {
|
2023-08-01 16:37:41 +08:00
|
|
|
|
|
2023-07-26 15:02:49 +08:00
|
|
|
|
// 在resolve时执行的回调函数
|
2023-07-28 09:42:53 +08:00
|
|
|
|
this.sqlData = value
|
2023-07-26 15:02:49 +08:00
|
|
|
|
console.log(value); // 输出:Hello, World!
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
// 在reject时执行的回调函数
|
|
|
|
|
console.error(error);
|
|
|
|
|
});
|
2023-07-26 14:31:09 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
</style>
|