<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/jsstore/dist/jsstore.min.js">
</script>
<script
src="https://cdn.jsdelivr.net/npm/jsstore/dist/jsstore.worker.min.js">
</script>
</head>
<body>
<input type="text" name="user" placeholder="user" id="user">
<input type="text" name="pass" placeholder="password" id="pass">
<input type="button" name="Submit" value="Submit"
onclick="addStudent();">
<script type="text/javascript">
var connection = new JsStore.Instance();
window.onload = function () {
initiateDatabase();
getDb();
}
function initiateDatabase(){
var DbName="ProductDatabase"
function initJsStore() {
connection.isDbExist(DbName).then(function(isExist) {
if (isExist) { // checks if the database exists, if it does then opens it, else creates a whole new dataBase
connection.openDb(DbName);
} else {
var database = getDbSchema1();
connection.createDb(database);
}
}).catch(function(err) {
console.error(err);
})
}
}
function getDb(){
var productTable = {
name: 'Product',
columns: [
{
name: 'Id',
primaryKey: true,
autoIncrement: true
},
{
name: 'Name',
notNull: true,
dataType: JsStore.DATA_TYPE.String
},
{
name: 'Password',
notNull: true,
dataType: JsStore.DATA_TYPE.Number
},
]
};
var db = {
name: "ProductDatabase",
tables: [productTable]
}
return db;
}
}
function addStudent() {
var UserName = document.getElementById("user").Value;
var Password= document.getElementById("pass").Value;
var Value = {
Name: UserName,
Password: Password
};
connection.insert({
into: 'Product',
values: [Value]
}).then(function (rowsAdded) {
if(rowsAdded > 0){
alert("Successfully Added");
}
}).catch(function (err) {
console.log(err);
alert(err.message);
})
}
</script>
我的代码有什么问题? 是否有缺少的东西? 我是第一次尝试使用JsStore建立索引数据库。
说明:
我正在尝试将用户名和密码添加到indexedDb,但尝试失败。请指出我的错误。我希望它完全使用JavaScript代码no(JQuery)。
答案 0 :(得分:0)
function initJsStore() {
var database = getDbSchema1();
//removed above all
因为它们是jsstore提供的示例代码,所以我已经复制了它,但从现在起我不需要它
var UserName = document.getElementById("user").Value; // Value should be in lower case.
我已经回顾了JsStore Developer创建的示例。