Skip to content

Commit d44fe50

Browse files
committed
doc: update the SQLite C interoperability example
1 parent 006d260 commit d44fe50

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

doc/docs.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,15 +1713,29 @@ fn main(){
17131713
#flag -lsqlite3
17141714
#include "sqlite3.h"
17151715
1716-
struct C.sqlite3
1717-
struct C.sqlite3_stmt
1718-
1719-
fn C.sqlite3_open(charptr, C.sqlite3)
1720-
fn C.sqlite3_column_int(stmt C.sqlite3_stmt, n int) int
1721-
// Or just define the type of parameter & leave C. prefix
1722-
fn C.sqlite3_prepare_v2(sqlite3, charptr, int, sqlite3_stmt, charptr) int
1723-
fn C.sqlite3_step(sqlite3)
1724-
fn C.sqlite3_finalize(sqlite3_stmt)
1716+
// See also the example from https://www.sqlite.org/quickstart.html
1717+
struct C.sqlite3{}
1718+
struct C.sqlite3_stmt{}
1719+
1720+
type FnSqlite3Callback fn(voidptr, int, &charptr, &charptr) int
1721+
1722+
fn C.sqlite3_open(charptr, &&C.sqlite3) int
1723+
fn C.sqlite3_close(&C.sqlite3) int
1724+
fn C.sqlite3_column_int(stmt &C.sqlite3_stmt, n int) int
1725+
// ... you can also just define the type of parameter & leave out the C. prefix
1726+
fn C.sqlite3_prepare_v2(&sqlite3, charptr, int, &&sqlite3_stmt, &charptr) int
1727+
fn C.sqlite3_step(&sqlite3_stmt)
1728+
fn C.sqlite3_finalize(&sqlite3_stmt)
1729+
fn C.sqlite3_exec(db &sqlite3, sql charptr, FnSqlite3Callback, cb_arg voidptr, emsg &charptr) int
1730+
fn C.sqlite3_free(voidptr)
1731+
1732+
fn my_callback(arg voidptr, howmany int, cvalues &charptr, cnames &charptr) int {
1733+
for i in 0..howmany {
1734+
print('| ${cstring_to_vstring(cnames[i])}: ${cstring_to_vstring(cvalues[i]):20} ')
1735+
}
1736+
println('|')
1737+
return 0
1738+
}
17251739
17261740
fn main() {
17271741
path := 'users.db'
@@ -1733,7 +1747,16 @@ fn main() {
17331747
C.sqlite3_step(stmt)
17341748
nr_users := C.sqlite3_column_int(stmt, 0)
17351749
C.sqlite3_finalize(stmt)
1736-
println(nr_users)
1750+
println('There are $nr_users users in the database.')
1751+
//
1752+
error_msg := charptr(0)
1753+
query_all_users := 'select * from users'
1754+
rc := C.sqlite3_exec(db, query_all_users.str, my_callback, 7, &error_msg)
1755+
if rc != C.SQLITE_OK {
1756+
eprintln( cstring_to_vstring(error_msg) )
1757+
C.sqlite3_free(error_msg)
1758+
}
1759+
C.sqlite3_close(db)
17371760
}
17381761
```
17391762

0 commit comments

Comments
 (0)