Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| reinforcedlearningwithaisupport:sqliteinthebrowserpoc [2025/03/08 17:11] – created gedbadmin | reinforcedlearningwithaisupport:sqliteinthebrowserpoc [2025/04/10 13:26] (current) – [The code] gedbadmin | ||
|---|---|---|---|
| Line 546: | Line 546: | ||
| This is how AI will boost developer productivity: | This is how AI will boost developer productivity: | ||
| + | |||
| + | ====== The code ====== | ||
| + | |||
| + | If you want to take a look at what has been crated so far, here it is: | ||
| + | |||
| + | I've taken out the large Base64 string. | ||
| + | |||
| + | <code html> | ||
| + | < | ||
| + | <html lang=" | ||
| + | < | ||
| + | <meta charset=" | ||
| + | <meta name=" | ||
| + | < | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | DROP TABLE IF EXISTS test; | ||
| + | CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT); | ||
| + | INSERT INTO test (name) VALUES (' | ||
| + | SELECT * FROM test; | ||
| + | </ | ||
| + | <button id=" | ||
| + | <pre id=" | ||
| + | |||
| + | <script src=" | ||
| + | < | ||
| + | const base64wasm = " | ||
| + | const binaryString = atob(base64wasm); | ||
| + | const bytes = new Uint8Array(binaryString.length); | ||
| + | for (let i = 0; i < binaryString.length; | ||
| + | bytes[i] = binaryString.charCodeAt(i); | ||
| + | } | ||
| + | |||
| + | (async () => { | ||
| + | // Initialize sql.js using the in-memory WASM binary | ||
| + | const sql = await initSqlJs({ | ||
| + | locateFile: () => { | ||
| + | // Create a blob for the binary and generate a URL | ||
| + | const blob = new Blob([bytes], | ||
| + | return URL.createObjectURL(blob); | ||
| + | } | ||
| + | }); | ||
| + | |||
| + | const db = new sql.Database(); | ||
| + | |||
| + | document.getElementById(' | ||
| + | const sqlQuery = document.getElementById(" | ||
| + | const result = db.exec(sqlQuery); | ||
| + | document.getElementById(' | ||
| + | }); | ||
| + | })(); | ||
| + | |||
| + | function resultToHTMLTable(result) { | ||
| + | if (!result || result.length === 0) { | ||
| + | return "< | ||
| + | } | ||
| + | // Use the first result set (if there are multiple) | ||
| + | const data = result[0]; | ||
| + | let html = "< | ||
| + | | ||
| + | // Create header row | ||
| + | data.columns.forEach(col => { | ||
| + | html += `< | ||
| + | }); | ||
| + | html += "</ | ||
| + | | ||
| + | // Create data rows | ||
| + | data.values.forEach(row => { | ||
| + | html += "< | ||
| + | row.forEach(cell => { | ||
| + | html += `< | ||
| + | }); | ||
| + | html += "</ | ||
| + | }); | ||
| + | | ||
| + | html += "</ | ||
| + | return html; | ||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||