Add DB example

This commit is contained in:
2024-06-11 21:23:18 +02:00
parent 315fbdcc4e
commit 17c0acb250
4 changed files with 47 additions and 18 deletions

View File

@@ -1,8 +1,35 @@
<script lang="ts">
import Router from '$router/Router.svelte'
import { ModeWatcher, mode } from 'mode-watcher'
import { mode, ModeWatcher } from 'mode-watcher'
import Header from '$lib/components/Header.svelte'
import { Toaster } from 'svelte-sonner'
import { onMount } from 'svelte'
import { appConfigDir } from '@tauri-apps/api/path'
import Database from 'tauri-plugin-sql-api'
onMount(async () => {
console.log(await appConfigDir())
window.invoke = window.__TAURI_INVOKE__
const db = await Database.load('sqlite:food.db')
await db.execute(
'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);'
)
type User = {
id: number
name: string
age: number
}
await db.execute('INSERT INTO users (name, age) VALUES (\'John\', 30);')
const user = await db.select<User>('SELECT * FROM users WHERE name = $1;', [
'John'
])
console.log(user)
})
</script>
<ModeWatcher defaultMode="dark" />