@@ -1713,15 +1713,29 @@ fn main(){
1713
1713
#flag -lsqlite3
1714
1714
#include "sqlite3.h"
1715
1715
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
+ }
1725
1739
1726
1740
fn main() {
1727
1741
path := 'users.db'
@@ -1733,7 +1747,16 @@ fn main() {
1733
1747
C.sqlite3_step(stmt)
1734
1748
nr_users := C.sqlite3_column_int(stmt, 0)
1735
1749
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)
1737
1760
}
1738
1761
```
1739
1762
0 commit comments