forked from simonw/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite-wasm.html
147 lines (137 loc) · 5.11 KB
/
sqlite-wasm.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pelican Sightings Query Tool</title>
<script src="https://cdn.jsdelivr.net/npm/@sqlite.org/sqlite-wasm@3.46.1-build4/sqlite-wasm/jswasm/sqlite3.mjs" type="module"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
}
textarea {
width: 100%;
height: 100px;
margin-bottom: 10px;
}
button {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
table {
border-collapse: collapse;
width: 100%;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #4CAF50;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
#loading {
display: none;
color: #4CAF50;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Pelican Sightings in Half Moon Bay</h1>
<div id="loading">Loading SQLite and initializing database...</div>
<textarea id="query" placeholder="Enter your SQL query here...">SELECT * FROM pelican_sightings;</textarea>
<button id="executeButton" onclick="executeQuery()" disabled>Execute Query</button>
<div id="result"></div>
<script type="module">
import sqlite3InitModule from 'https://cdn.jsdelivr.net/npm/@sqlite.org/sqlite-wasm@3.46.1-build4/sqlite-wasm/jswasm/sqlite3.mjs';
const loadingElement = document.getElementById('loading');
const executeButton = document.getElementById('executeButton');
const initializeSQLite = async () => {
loadingElement.style.display = 'block';
try {
const sqlite3 = await sqlite3InitModule({
print: console.log,
printErr: console.error,
});
console.log('Running SQLite version', sqlite3.version.libVersion);
window.db = new sqlite3.oo1.DB();
window.db.exec(`
CREATE TABLE pelican_sightings (
id INTEGER PRIMARY KEY,
date TEXT,
location TEXT,
species TEXT,
count INTEGER
);
INSERT INTO pelican_sightings (date, location, species, count) VALUES
('2024-10-01', 'Miramar Beach', 'Brown Pelican', 12),
('2024-10-02', 'Pillar Point Harbor', 'California Brown Pelican', 8),
('2024-10-03', 'Half Moon Bay State Beach', 'American White Pelican', 3),
('2024-10-04', 'Dunes Beach', 'Brown Pelican', 15),
('2024-10-05', 'Poplar Beach', 'California Brown Pelican', 10);
`);
console.log('Database initialized with sample data');
executeButton.disabled = false;
} catch (err) {
console.error('Initialization error:', err.message);
document.getElementById('result').innerHTML = `<p>Error initializing database: ${err.message}</p>`;
} finally {
loadingElement.style.display = 'none';
}
};
initializeSQLite();
window.executeQuery = () => {
const query = document.getElementById('query').value;
try {
const results = window.db.selectObjects(query);
displayResults(results);
} catch (err) {
document.getElementById('result').innerHTML = `<p>Error: ${err.message}</p>`;
}
};
function displayResults(results) {
if (results.length === 0) {
document.getElementById('result').innerHTML = '<p>No results.</p>';
return;
}
let columns = Object.keys(results[0]);
let tableHtml = '<table><tr>';
columns.forEach(column => {
tableHtml += `<th>${column}</th>`;
});
tableHtml += '</tr>';
results.forEach(row => {
tableHtml += '<tr>';
columns.forEach(column => {
tableHtml += `<td>${row[column]}</td>`;
});
tableHtml += '</tr>';
});
tableHtml += '</table>';
document.getElementById('result').innerHTML = tableHtml;
}
</script>
</body>
</html>