1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
var db = openDatabase("mydb","1.0","test DB",2*1024*1024); console.log(db);
db.transaction(tx=>{ tx.executeSql("'CREATE TABLE IF NOT EXISTS LOGS (id unique, log)'") })
db.transaction(tx=>{ tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)'); tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "菜鸟教程")'); tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "www.runoob.com")'); })
var e_id = "a",e_log="b"; db.transaction(tx=>{ tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)'); tx.executeSql('INSERT INTO LOGS (id,log) VALUES (?, ?)', [e_id, e_log]); });
db.transaction(tx=>{ tx.executeSql("SELECT * FROM LOGS",[],(tx,result)=>{ var len = result.rows.length,i; var msg = `<p>查询记录条数:${len}</p>`; document.querySelector("#result").innerHTML += msg; for (var i = 0; i < len; i++) { console.log(result.rows.item(i).log); } }) })
db.transaction(tx=>{ tx.executeSql("DELETE FROM LOGS WHERE id=1"); })
db.transaction(tx=>{ tx.executeSql("DELETE FROM LOGS WHERE id=?",["a"]); })
db.transaction(tx=>{ tx.executeSql("UPDATE LOGS SET log='www.baidu.com/' WHERE id=2"); })
db.transaction(tx=>{ tx.executeSql("UPDATE LOGS SET log='www.google.com/' WHERE id=?",[2]); })
var db = openDatabase("mydb","1.0","",2*1024*1024); db.transaction(tx=>{ tx.executeSql("CREATE TABLE LOGS (id,log)"); tx.executeSql("INSERT INTO LOGS(id,log) VALUES(1,'hello world')"); tx.executeSql("INSERT INTO LOGS(id,log) VALUES(2,'www.baidu.com')"); var msg = "已经创建LOGS数据表,并插入两条数据"; console.log(msg); }) db.transaction(tx=>{ tx.executeSql("SELECT * FROM LOGS",[],(tx,results)=>{ var len = results.rows.length; console.log(`已经获取到${len}条数据`); console.log(results); for(var i = 0;i < len;i++){ console.log(results.rows[i].id,results.rows[i].log); } },null) })
var db = openDatabase("mydb","1.0","",2*1024*1024); document.querySelector("#create").addEventListener("click",function(){ db.transaction(tx=>{ tx.executeSql("CREATE TABLE IF NOT EXISTS LOGS(id,log)"); tx.executeSql("INSERT INTO LOGS(id,log) VALUES(?,?)",[1,'hello']); tx.executeSql("INSERT INTO LOGS(id,log) VALUES(?,?)",[2,'world']); console.log("已经新建数据库并上传两条数据"); }) },false) document.querySelector("#update").addEventListener("click",function(){ db.transaction(tx=>{ tx.executeSql("UPDATE LOGS SET log='www.google.com' WHERE id=?",[1]); }) },false) document.querySelector("#delete").addEventListener("click",function(){ db.transaction(tx=>{ tx.executeSql("DELETE FROM LOGS WHERE id=?",[1]); }) },false) document.querySelector("#find").addEventListener("click",function(){ db.transaction(tx=>{ tx.executeSql("SELECT * FROM LOGS",[],(tx,results)=>{ var len = results.rows.length; console.log(results); for(var i = 0;i < len;i++){ console.log(results.rows[i].id,results.rows[i].log); } }) }) },false)
|