OLD | NEW |
1 <html> | 1 <html> |
2 <script> | 2 <script> |
3 | 3 |
4 // Open a Web SQL database. | 4 // Open a Web SQL database. |
5 var g_db = null; | 5 var g_db = null; |
6 if (typeof window.openDatabase == "undefined") { | 6 if (typeof window.openDatabase == "undefined") { |
7 document.write("Error: Web SQL databases are not supported."); | 7 document.write("Error: Web SQL databases are not supported."); |
8 } | 8 } |
9 try { | 9 try { |
10 g_db = openDatabase("test", "1.0", "test database", 1024 * 1024); | 10 g_db = openDatabase("test", "1.0", "test database", 1024 * 1024); |
(...skipping 24 matching lines...) Expand all Loading... |
35 tx.executeSql("INSERT INTO table1 VALUES (?)", [text]); | 35 tx.executeSql("INSERT INTO table1 VALUES (?)", [text]); |
36 }, | 36 }, |
37 function(error) { | 37 function(error) { |
38 sendValueToTest(error); | 38 sendValueToTest(error); |
39 }, | 39 }, |
40 function() { | 40 function() { |
41 sendValueToTest("done"); | 41 sendValueToTest("done"); |
42 }); | 42 }); |
43 } | 43 } |
44 | 44 |
| 45 |
| 46 // Updates a record at the given index with the given text. The indices are |
| 47 // 0-based and are ordered from oldest record, to newest record. |
| 48 function updateRecord(index, text) { |
| 49 var didUpdate = false; |
| 50 findId(index, function(rowId) { |
| 51 g_db.transaction( |
| 52 function(tx) { |
| 53 tx.executeSql( |
| 54 "UPDATE table1 SET data=? WHERE ROWID=?", |
| 55 [text, rowId], |
| 56 function(tx, result) { |
| 57 if (result.rowsAffected == 1) |
| 58 didUpdate = true; |
| 59 else if (result.rowsAffected == 0) |
| 60 sendValueToTest("could not update index: " + index); |
| 61 else |
| 62 sendValueToTest("multiple rows with index: " + index); |
| 63 }); |
| 64 }, |
| 65 function(error) { |
| 66 sendValueToTest("update error: " + error); |
| 67 }, |
| 68 function() { |
| 69 if (didUpdate) |
| 70 sendValueToTest("done"); |
| 71 }); |
| 72 }); |
| 73 } |
| 74 |
| 75 // Deletes a record at the given index. |
| 76 function deleteRecord(index) { |
| 77 findId(index, function(rowId) { |
| 78 g_db.transaction( |
| 79 function(tx) { |
| 80 tx.executeSql("DELETE FROM table1 WHERE ROWID=?", [rowId]); |
| 81 }, |
| 82 function(error) { |
| 83 sendValueToTest("delete error: " + error); |
| 84 }, |
| 85 function() { |
| 86 sendValueToTest("done"); |
| 87 }); |
| 88 }); |
| 89 } |
| 90 |
45 // Gets all the records in the database, ordered by their age. | 91 // Gets all the records in the database, ordered by their age. |
46 function getRecords() { | 92 function getRecords() { |
47 g_db.readTransaction(function(tx) { | 93 g_db.readTransaction(function(tx) { |
48 tx.executeSql( | 94 tx.executeSql( |
49 "SELECT data FROM table1 ORDER BY ROWID", | 95 "SELECT data FROM table1 ORDER BY ROWID", |
50 [], | 96 [], |
51 function(tx, result) { | 97 function(tx, result) { |
52 items = ""; | 98 items = ""; |
53 for (var i = 0; i < result.rows.length; i++) { | 99 for (var i = 0; i < result.rows.length; i++) { |
54 if (items != "") | 100 if (items != "") |
55 items += ", "; | 101 items += ", "; |
56 items += result.rows.item(i).data; | 102 items += result.rows.item(i).data; |
57 } | 103 } |
58 sendValueToTest(items); | 104 sendValueToTest(items); |
59 }, | 105 }, |
60 function(tx, error) { | 106 function(tx, error) { |
61 sendValueToTest(error); | 107 sendValueToTest("getRecords error: " + error); |
| 108 }); |
| 109 }); |
| 110 } |
| 111 |
| 112 // Helper function that finds the ID for a record based on a given index. |
| 113 function findId(index, callback) { |
| 114 g_db.readTransaction(function(tx) { |
| 115 // |ROWID| is a special sqlite column. It is unique and is incremented |
| 116 // automatically when a new record is created. |
| 117 // |LIMIT| is a nonstandard clause supported by sqlite that lets us pick |
| 118 // rows from the database by index. E.g., LIMIT 2,10 will give us 10 records |
| 119 // starting at offset 2. |
| 120 tx.executeSql( |
| 121 "SELECT ROWID AS id FROM table1 ORDER BY ROWID LIMIT ?,1", |
| 122 [index], |
| 123 function(tx, result) { |
| 124 if (result.rows.length >= 1) |
| 125 callback(result.rows.item(0).id); |
| 126 else |
| 127 sendValueToTest("could not find row with index: " + index); |
| 128 }, |
| 129 function(tx, error) { |
| 130 sendValueToTest("findId error: " + error); |
62 }); | 131 }); |
63 }); | 132 }); |
64 } | 133 } |
65 | 134 |
66 function sendValueToTest(value) { | 135 function sendValueToTest(value) { |
67 //alert(value); | 136 //alert(value); |
68 window.domAutomationController.setAutomationId(0); | 137 window.domAutomationController.setAutomationId(0); |
69 window.domAutomationController.send(value); | 138 window.domAutomationController.send(value); |
70 } | 139 } |
71 | 140 |
72 </script> | 141 </script> |
73 | 142 |
74 <body> | 143 <body> |
75 This page is used for testing Web SQL databases. | 144 This page is used for testing Web SQL databases. |
76 </body> | 145 </body> |
77 </html> | 146 </html> |
OLD | NEW |