javascript - WebSQL Doing Strange things when adapting code -
i have adapted code try create multiple columns in websql database. seems create tables, not insert when told to. changed code number or columns create , insert (i think). can me diagnose why won't insert
<script> //test browser compatibility if (window.opendatabase) { //create database parameters 1. database name 2.version number 3. description 4. size of database (in bytes) 1024 x 1024 = 1mb var mydb = opendatabase("cars_db", "0.1", "a database of cars like", 1024 * 1024); //create cars table using sql database using 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 not supported browser!"); } //function output list of cars in database function updatecarlist(transaction, results) { //initialise listitems variable var listitems = ""; //get car list holder ul var listholder = document.getelementbyid("receipt"); //clear cars list ul listholder.innerhtml = ""; var i; //iterate through results (i = 0; < results.rows.length; i++) { //get 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 list of cars database function outputcars() { //check ensure mydb object has been created if (mydb) { //get cars database select statement, set outputcarlist callback function executesql command mydb.transaction(function (t) { t.executesql("select * cars", [], updatecarlist); }); } else { alert("db not found, browser not support web sql!"); } } //function add car database function addcar() { //check ensure mydb object has been created if (mydb) { //get values of make , model text inputs var item1 = "hamburger"; var chair = "cheese"; var table = "123"; //test ensure user has entered both make , model if (item1 !== "" && chair !== "") { //insert user entered details cars table, note use of ? placeholder, these replaced data passed in array second parameter mydb.transaction(function (t) { t.executesql("insert cars (item1, chair, table) values (?,?,?)", [item1, chair, table]); outputcars(); }); } else { alert("you must enter make , model!"); } } else { alert("db not found, browser not support web sql!"); } } //function remove car database, passed row id it's parameter function deletecar(id) { //check ensure mydb object has been created if (mydb) { //get cars database select statement, set outputcarlist callback function executesql command mydb.transaction(function (t) { t.executesql("delete cars id=?", [id], outputcars); }); } else { alert("db not found, browser not support web sql!"); } } outputcars(); </script>
here code adapted (this 1 works fine!)
//test browser compatibility if (window.opendatabase) { //create database parameters 1. database name 2.version number 3. description 4. size of database (in bytes) 1024 x 1024 = 1mb var mydb = opendatabase("cars_db", "0.1", "a database of cars like", 1024 * 1024); //create cars table using sql database using 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 not supported browser!"); } //function output list of cars in database function updatecarlist(transaction, results) { //initialise listitems variable var listitems = ""; //get car list holder ul var listholder = document.getelementbyid("carlist"); //clear cars list ul listholder.innerhtml = ""; var i; //iterate through results (i = 0; < results.rows.length; i++) { //get 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 list of cars database function outputcars() { //check ensure mydb object has been created if (mydb) { //get cars database select statement, set outputcarlist callback function executesql command mydb.transaction(function (t) { t.executesql("select * cars", [], updatecarlist); }); } else { alert("db not found, browser not support web sql!"); } } //function add car database function addcar() { //check ensure mydb object has been created if (mydb) { //get values of make , model text inputs var make = document.getelementbyid("carmake").value; var model = document.getelementbyid("carmodel").value; //test ensure user has entered both make , model if (make !== "" && model !== "") { //insert user entered details cars table, note use of ? placeholder, these replaced data passed in array second parameter mydb.transaction(function (t) { t.executesql("insert cars (make, model) values (?, ?)", [make, model]); outputcars(); }); } else { alert("you must enter make , model!"); } } else { alert("db not found, browser not support web sql!"); } } //function remove car database, passed row id it's parameter function deletecar(id) { //check ensure mydb object has been created if (mydb) { //get cars database select statement, set outputcarlist callback function executesql command mydb.transaction(function (t) { t.executesql("delete cars id=?", [id], outputcars); }); } else { alert("db not found, browser not support web sql!"); } } outputcars();
if modify make/model part specific value rather getting input, works fine...its after try adding more columns.
Comments
Post a Comment