ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 概述 一个带有 SQL 语法的 IndexedDB 包装器。 ## demo ``` // 创建数据库 var dbName ='JsStore_Demo'; var tblProduct = { name: 'Product', columns: { // Here "Id" is name of column id:{ primaryKey: true, autoIncrement: true }, itemName: { notNull: true, dataType: "string" }, price: { notNull: true, dataType: "number" }, quantity : { notNull: true, dataType: "number" } } }; var database = { name: dbName, tables: [tblProduct] } // 初始化数据库 const isDbCreated = await connection.initDb(database); if(isDbCreated === true){ console.log("db created"); // here you can prefill database with some data } else { console.log("db opened"); } // 生成数据 var value = { itemName: 'Blue Jeans', price: 2000, quantity: 1000 } var insertCount = await connection.insert({ into: 'Product', values: [value] }); console.log(`${insertCount} rows inserted`); // 读数据 // results will be array of objects var results = await connection.select({ from: 'Product', where: { id: 5 } }); alert(results.length + 'record found'); // 更新数据 var rowsUpdated = await connection.update({ in: 'Product', where: { itemName: { like: '%black%' } }, set: { quantity: 2000 } }); alert(rowsUpdated + ' rows updated'); // 删除数据 var rowsDeleted = await connection.remove({ from: 'Product', where: { id: 10 } }); alert(rowsDeleted + ' record deleted'); ```