demo: sqlite

This commit is contained in:
xushiwei
2024-05-08 10:43:10 +08:00
parent 52dcfaa452
commit f0e3e556cf
4 changed files with 161 additions and 71 deletions

View File

@@ -1,19 +1,3 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (

59
_demo/sqlite/sqlite.go Normal file
View File

@@ -0,0 +1,59 @@
package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/x/sqlite"
)
func main() {
db, err := sqlite.OpenV2(c.Str(":memory:"), sqlite.OpenReadWrite|sqlite.OpenMemory, nil)
check(err)
err = db.Exec(c.Str("CREATE TABLE foo (id INT, name TEXT)"), nil, nil, nil)
check(err)
stmt, err := db.PrepareV3("INSERT INTO foo VALUES (?, ?)", 0, nil)
check(err)
stmt.BindInt(1, 100)
stmt.BindText(2, c.Str("Hello World"), sqlite.Static, nil)
err = stmt.Step()
checkDone(err)
stmt.Reset()
stmt.BindInt(1, 200)
stmt.BindText(2, c.Str("This is llgo"), sqlite.Static, nil)
err = stmt.Step()
checkDone(err)
stmt.Close()
stmt, err = db.PrepareV3("SELECT * FROM foo", 0, nil)
check(err)
for {
if err = stmt.Step(); err != sqlite.HasRow {
break
}
c.Printf(c.Str("==> id=%d, name=%s\n"), stmt.ColumnInt(0), stmt.ColumnText(1))
}
checkDone(err)
stmt.Close()
db.Close()
}
func check(err sqlite.Errno) {
if err != sqlite.OK {
c.Printf(c.Str("==> Error: (%d) %s\n"), err, err.Errstr())
c.Exit(1)
}
}
func checkDone(err sqlite.Errno) {
if err != sqlite.Done {
check(err)
}
}