Javascript WebSQL在调整代码时会做一些奇怪的事情
我修改了这段代码,试图在WebSQL数据库中创建多个列。它似乎在创建表,但在被告知时不会插入。我对代码所做的更改是创建和插入的数字或列(我想)。有人能帮我诊断为什么它不能插入吗Javascript WebSQL在调整代码时会做一些奇怪的事情,javascript,mysql,sql,google-chrome,Javascript,Mysql,Sql,Google Chrome,我修改了这段代码,试图在WebSQL数据库中创建多个列。它似乎在创建表,但在被告知时不会插入。我对代码所做的更改是创建和插入的数字或列(我想)。有人能帮我诊断为什么它不能插入吗 <script> //Test for browser compatibility if (window.openDatabase) { //Create the database the parameters are 1. the database name 2.version number
<script>
//Test for browser compatibility
if (window.openDatabase) {
//Create the database the parameters are 1. the database name 2.version number 3. a description 4. the size of the database (in bytes) 1024 x 1024 = 1MB
var mydb = openDatabase("cars_db", "0.1", "A Database of Cars I Like", 1024 * 1024);
//create the cars table using SQL for the database using a transaction
mydb.transaction(function (t) {
t.executeSql("CREATE TABLE IF NOT EXISTS cars (id INTEGER PRIMARY KEY ASC, item1 TEXT, chair TEXT, table TEXT)");
});
} else {
alert("WebSQL is not supported by your browser!");
}
//function to output the list of cars in the database
function updateCarList(transaction, results) {
//initialise the listitems variable
var listitems = "";
//get the car list holder ul
var listholder = document.getElementById("receipt");
//clear cars list ul
listholder.innerHTML = "";
var i;
//Iterate through the results
for (i = 0; i < results.rows.length; i++) {
//Get the current row
var row = results.rows.item(i);
listholder.innerHTML += "<li>" + row.item1 + " - " + row.chair + " (<a href='javascript:void(0);' onclick='deleteCar(" + row.id + ");'>Delete Car</a>)";
}
}
//function to get the list of cars from the database
function outputCars() {
//check to ensure the mydb object has been created
if (mydb) {
//Get all the cars from the database with a select statement, set outputCarList as the callback function for the executeSql command
mydb.transaction(function (t) {
t.executeSql("SELECT * FROM cars", [], updateCarList);
});
} else {
alert("db not found, your browser does not support web sql!");
}
}
//function to add the car to the database
function addCar() {
//check to ensure the mydb object has been created
if (mydb) {
//get the values of the make and model text inputs
var item1 = "hamburger";
var chair = "cheese";
var table = "123";
//Test to ensure that the user has entered both a make and model
if (item1 !== "" && chair !== "") {
//Insert the user entered details into the cars table, note the use of the ? placeholder, these will replaced by the data passed in as an array as the second parameter
mydb.transaction(function (t) {
t.executeSql("INSERT INTO cars (item1, chair, table) VALUES (?,?,?)", [item1, chair, table]);
outputCars();
});
} else {
alert("You must enter a make and model!");
}
} else {
alert("db not found, your browser does not support web sql!");
}
}
//function to remove a car from the database, passed the row id as it's only parameter
function deleteCar(id) {
//check to ensure the mydb object has been created
if (mydb) {
//Get all the cars from the database with a select statement, set outputCarList as the callback function for the executeSql command
mydb.transaction(function (t) {
t.executeSql("DELETE FROM cars WHERE id=?", [id], outputCars);
});
} else {
alert("db not found, your browser does not support web sql!");
}
}
outputCars();
</script>
//浏览器兼容性测试
if(window.openDatabase){
//创建数据库参数为1.数据库名称2.版本号3.描述4.数据库大小(字节)1024 x 1024=1MB
var mydb=openDatabase(“cars_db”,“0.1”,“我喜欢的汽车数据库”,1024*1024);
//使用事务为数据库使用SQL创建cars表
mydb.transaction(函数(t){
t、 executeSql(“如果不存在,则创建表cars(id整数主键ASC,item1文本,椅子文本,表文本)”);
});
}否则{
警报(“您的浏览器不支持WebSQL!”);
}
//函数输出数据库中的车辆列表
函数updateCarList(事务、结果){
//初始化listitems变量
var listitems=“”;
//获取汽车列表持有者ul
var listholder=document.getElementById(“收据”);
//无障碍汽车清单
listholder.innerHTML=“”;
var i;
//反复浏览结果
对于(i=0;i”+row.item1+”-“+row.chair+”()”;
}
}
//函数从数据库中获取车辆列表
函数outputCars(){
//检查以确保已创建mydb对象
如果(mydb){
//使用select语句从数据库中获取所有汽车,将outputCarList设置为executeSql命令的回调函数
mydb.transaction(函数(t){
t、 executeSql(“从汽车中选择*,[],更新carlist);
});
}否则{
警报(“未找到数据库,您的浏览器不支持web sql!”);
}
}
//函数将汽车添加到数据库中
函数addCar(){
//检查以确保已创建mydb对象
如果(mydb){
//获取make和model文本输入的值
var item1=“汉堡”;
var chair=“cheese”;
var table=“123”;
//测试以确保用户已输入品牌和型号
如果(项目1!==“”&椅子!==“”){
//将用户输入的详细信息插入cars表,注意?占位符的使用,这些将被作为第二个参数作为数组传入的数据替换
mydb.transaction(函数(t){
t、 executeSql(“插入汽车(项目1,椅子,表格)值(?,,?)”,[item1,椅子,表格];
输出车();
});
}否则{
警报(“您必须输入品牌和型号!”);
}
}否则{
警报(“未找到数据库,您的浏览器不支持web sql!”);
}
}
//函数从数据库中删除汽车,将行id作为其唯一参数传递
功能删除车(id){
//检查以确保已创建mydb对象
如果(mydb){
//使用select语句从数据库中获取所有汽车,将outputCarList设置为executeSql命令的回调函数
mydb.transaction(函数(t){
t、 executeSql(“从id=?,[id],outputCars的车辆中删除”);
});
}否则{
警报(“未找到数据库,您的浏览器不支持web sql!”);
}
}
输出车();
下面是我改编的代码(这个很好用!)
//测试浏览器兼容性
if(window.openDatabase){
//创建数据库参数为1.数据库名称2.版本号3.描述4.数据库大小(字节)1024 x 1024=1MB
var mydb=openDatabase(“cars_db”,“0.1”,“我喜欢的汽车数据库”,1024*1024);
//使用事务为数据库使用SQL创建cars表
mydb.transaction(函数(t){
t、 executeSql(“如果不存在,则创建表cars(id整数主键ASC、生成文本、模型文本)”);
});
}否则{
警报(“您的浏览器不支持WebSQL!”);
}
//函数输出数据库中的车辆列表
函数updateCarList(事务、结果){
//初始化listitems变量
var listitems=“”;
//获取汽车列表持有者ul
var listholder=document.getElementById(“carlist”);
//无障碍汽车清单
listholder.innerHTML=“”;
var i;
//反复浏览结果
对于(i=0;i”+row.make+”-“+row.model+”()”;
}
}
//函数从数据库中获取车辆列表
函数outputCars(){
//检查以确保已创建mydb对象
如果(mydb){
//使用select语句从数据库中获取所有汽车,将outputCarList设置为executeSql命令的回调函数
mydb.transaction(函数(t){
t、 executeSql(“从汽车中选择*,[],更新carlist);
});
}否则{
警报(“未找到数据库,您的浏览器不支持web sql!”);
}
}
//函数将汽车添加到数据库中
函数addCar(){
//检查以确保已创建mydb对象
如果(mydb){
//获取make和model文本输入的值
var make=document.getElementById(“carmake”).value;
var模型=document.getElementById(“carmodel”).value;
//测试以确保用户已输入品牌和型号
如果(制造!=“”&&model!=“”){
//将用户输入的详细信息插入cars表,注意?占位符的使用,这些将被作为第二个参数作为数组传入的数据替换
mydb.transaction(函数(t){
t、 executeSql(“插入汽车(制造、,
//Test for browser compatibility
if (window.openDatabase) {
//Create the database the parameters are 1. the database name 2.version number 3. a description 4. the size of the database (in bytes) 1024 x 1024 = 1MB
var mydb = openDatabase("cars_db", "0.1", "A Database of Cars I Like", 1024 * 1024);
//create the cars table using SQL for the database using a transaction
mydb.transaction(function (t) {
t.executeSql("CREATE TABLE IF NOT EXISTS cars (id INTEGER PRIMARY KEY ASC, make TEXT, model TEXT)");
});
} else {
alert("WebSQL is not supported by your browser!");
}
//function to output the list of cars in the database
function updateCarList(transaction, results) {
//initialise the listitems variable
var listitems = "";
//get the car list holder ul
var listholder = document.getElementById("carlist");
//clear cars list ul
listholder.innerHTML = "";
var i;
//Iterate through the results
for (i = 0; i < results.rows.length; i++) {
//Get the current row
var row = results.rows.item(i);
listholder.innerHTML += "<li>" + row.make + " - " + row.model + " (<a href='javascript:void(0);' onclick='deleteCar(" + row.id + ");'>Delete Car</a>)";
}
}
//function to get the list of cars from the database
function outputCars() {
//check to ensure the mydb object has been created
if (mydb) {
//Get all the cars from the database with a select statement, set outputCarList as the callback function for the executeSql command
mydb.transaction(function (t) {
t.executeSql("SELECT * FROM cars", [], updateCarList);
});
} else {
alert("db not found, your browser does not support web sql!");
}
}
//function to add the car to the database
function addCar() {
//check to ensure the mydb object has been created
if (mydb) {
//get the values of the make and model text inputs
var make = document.getElementById("carmake").value;
var model = document.getElementById("carmodel").value;
//Test to ensure that the user has entered both a make and model
if (make !== "" && model !== "") {
//Insert the user entered details into the cars table, note the use of the ? placeholder, these will replaced by the data passed in as an array as the second parameter
mydb.transaction(function (t) {
t.executeSql("INSERT INTO cars (make, model) VALUES (?, ?)", [make, model]);
outputCars();
});
} else {
alert("You must enter a make and model!");
}
} else {
alert("db not found, your browser does not support web sql!");
}
}
//function to remove a car from the database, passed the row id as it's only parameter
function deleteCar(id) {
//check to ensure the mydb object has been created
if (mydb) {
//Get all the cars from the database with a select statement, set outputCarList as the callback function for the executeSql command
mydb.transaction(function (t) {
t.executeSql("DELETE FROM cars WHERE id=?", [id], outputCars);
});
} else {
alert("db not found, your browser does not support web sql!");
}
}
outputCars();