Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler demo #603

Open
Sbrjt opened this issue Mar 11, 2025 · 0 comments
Open

Simpler demo #603

Sbrjt opened this issue Mar 11, 2025 · 0 comments

Comments

@Sbrjt
Copy link

Sbrjt commented Mar 11, 2025

The sample demo in the readme seems intimidating...

Here's a simpler one:

<head>
    <title>Demo</title>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.12.0/sql-wasm.js"></script>
    <!-- preload sql-wasm.wasm, if you like -->
</head>

<body>
    <button id='btn'>Run query</button>

    <script type='module'>
        // Initialize SQL.js
        const SQL = await initSqlJs({
            locateFile: () => `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.12.0/sql-wasm.wasm`
        })

        // Create a db with sample data
        const db = new SQL.Database()

        db.run(`CREATE TABLE test (id INTEGER, name TEXT); INSERT INTO test VALUES (1, 'Alice'), (2, 'Bob')`)

        // Run a query
        async function run() {
            const result = await db.exec("SELECT * FROM test")
            console.log(result[0].values)
        }

        btn.addEventListener('click', run)
    </script>
</body>

For previous versions:

<head>
    <title>Demo</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/sql.js/0.5.0/js/sql-optimized.js"></script>
</head>

<body>
    <button id='btn'>Run query</button>

    <script type='module'>
        const db = new SQL.Database()
        db.run(`CREATE TABLE test (id INTEGER, name TEXT); INSERT INTO test VALUES (1, 'Alice'), (2, 'Bob')`)

        async function run() {
            const result = await db.exec("SELECT * FROM test")
            console.log(result[0].values)
        }

        btn.addEventListener('click', run)
    </script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant