forked from nanodeath/JS-Migrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
63 lines (61 loc) · 2.14 KB
/
test.html
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
<!DOCTYPE html>
<html>
<head>
<title>Migrator Test</title>
</head>
<body>
<!-- jQuery isn't required for migrator.js, just for my example code
below. You can use any JS library you want. -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
<script type="text/javascript" src="migrator.js"></script>
<script type="text/javascript">
if(window.openDatabase){
var db = openDatabase("example_db", "", "Example Database", 100000);
// now db is a database object: http://dev.w3.org/html5/webdatabase/#database
var M = new Migrator(db);
M.setDebugLevel(Migrator.DEBUG_HIGH);
M.migration(1, function(t){
// t here is a transaction object: http://dev.w3.org/html5/webdatabase/#sqltransaction
t.executeSql("create table user(id integer primary key, name text)");
t.executeSql("insert into user(name) values('max')");
});
M.migration(2, function(t){
t.executeSql("alter table user add column phone text");
t.executeSql("update user set phone = '555-5555' where name == 'max'");
});
M.migration(3, function(t){
t.executeSql("insert into user(name, phone) values('jeremy', '555-1234')");
t.executeSql("update user set phone = '555-5556' where name == 'max'");
});
// This executes the applicable transactions
M.execute();
} else {
$("p:eq(0)").text("Oops! HTML5 databases are not supported with your browser.");
}
</script>
<h1>Address Book</h1>
<p>Here are all the numbers in the address book...</p>
<table class="address_book">
<tr>
<th>ID</th>
<th>Name</th>
<th>Phone</th>
</tr>
</table>
<script type="text/javascript">
M.whenDone(function(){
db.transaction(function(t){
t.executeSql("select id, name, phone from user", [], function(t, res){
var rows = res.rows;
var addressTable = $("table.address_book");
for(var i = 0; i < rows.length; i++){
var row = rows.item(i);
var domRow = $("<tr><td>"+row.id+"</td><td>"+row.name+"</td><td>"+row.phone+"</td></tr>");
domRow.appendTo(addressTable);
}
});
});
});
</script>
</body>
</html>