ci: fix compilation and formatting of sqlite example in docs.md
parent
92fa9bbea9
commit
34aa67b1e8
18
doc/docs.md
18
doc/docs.md
|
@ -3306,26 +3306,26 @@ struct C.sqlite3 {
|
||||||
struct C.sqlite3_stmt {
|
struct C.sqlite3_stmt {
|
||||||
}
|
}
|
||||||
|
|
||||||
type FnSqlite3Callback = fn (voidptr, int, &charptr, &charptr) int
|
type FnSqlite3Callback = fn (voidptr, int, &&char, &&char) int
|
||||||
|
|
||||||
fn C.sqlite3_open(charptr, &&C.sqlite3) int
|
fn C.sqlite3_open(&char, &&C.sqlite3) int
|
||||||
|
|
||||||
fn C.sqlite3_close(&C.sqlite3) int
|
fn C.sqlite3_close(&C.sqlite3) int
|
||||||
|
|
||||||
fn C.sqlite3_column_int(stmt &C.sqlite3_stmt, n int) int
|
fn C.sqlite3_column_int(stmt &C.sqlite3_stmt, n int) int
|
||||||
|
|
||||||
// ... you can also just define the type of parameter and leave out the C. prefix
|
// ... you can also just define the type of parameter and leave out the C. prefix
|
||||||
fn C.sqlite3_prepare_v2(&C.sqlite3, charptr, int, &&C.sqlite3_stmt, &charptr) int
|
fn C.sqlite3_prepare_v2(&C.sqlite3, &char, int, &&C.sqlite3_stmt, &&char) int
|
||||||
|
|
||||||
fn C.sqlite3_step(&C.sqlite3_stmt)
|
fn C.sqlite3_step(&C.sqlite3_stmt)
|
||||||
|
|
||||||
fn C.sqlite3_finalize(&C.sqlite3_stmt)
|
fn C.sqlite3_finalize(&C.sqlite3_stmt)
|
||||||
|
|
||||||
fn C.sqlite3_exec(db &C.sqlite3, sql charptr, cb FnSqlite3Callback, cb_arg voidptr, emsg &charptr) int
|
fn C.sqlite3_exec(db &C.sqlite3, sql &char, cb FnSqlite3Callback, cb_arg voidptr, emsg &&char) int
|
||||||
|
|
||||||
fn C.sqlite3_free(voidptr)
|
fn C.sqlite3_free(voidptr)
|
||||||
|
|
||||||
fn my_callback(arg voidptr, howmany int, cvalues &charptr, cnames &charptr) int {
|
fn my_callback(arg voidptr, howmany int, cvalues &&char, cnames &&char) int {
|
||||||
unsafe {
|
unsafe {
|
||||||
for i in 0 .. howmany {
|
for i in 0 .. howmany {
|
||||||
print('| ${cstring_to_vstring(cnames[i])}: ${cstring_to_vstring(cvalues[i]):20} ')
|
print('| ${cstring_to_vstring(cnames[i])}: ${cstring_to_vstring(cvalues[i]):20} ')
|
||||||
|
@ -3344,17 +3344,17 @@ fn main() {
|
||||||
stmt := &C.sqlite3_stmt(0)
|
stmt := &C.sqlite3_stmt(0)
|
||||||
// NB: you can also use the `.str` field of a V string,
|
// NB: you can also use the `.str` field of a V string,
|
||||||
// to get its C style zero terminated representation
|
// to get its C style zero terminated representation
|
||||||
C.sqlite3_prepare_v2(db, query.str, -1, &stmt, 0)
|
C.sqlite3_prepare_v2(db, &char(query.str), -1, &stmt, 0)
|
||||||
C.sqlite3_step(stmt)
|
C.sqlite3_step(stmt)
|
||||||
nr_users := C.sqlite3_column_int(stmt, 0)
|
nr_users := C.sqlite3_column_int(stmt, 0)
|
||||||
C.sqlite3_finalize(stmt)
|
C.sqlite3_finalize(stmt)
|
||||||
println('There are $nr_users users in the database.')
|
println('There are $nr_users users in the database.')
|
||||||
//
|
//
|
||||||
error_msg := charptr(0)
|
error_msg := &char(0)
|
||||||
query_all_users := 'select * from users'
|
query_all_users := 'select * from users'
|
||||||
rc := C.sqlite3_exec(db, query_all_users.str, my_callback, 7, &error_msg)
|
rc := C.sqlite3_exec(db, &char(query_all_users.str), my_callback, voidptr(7), &error_msg)
|
||||||
if rc != C.SQLITE_OK {
|
if rc != C.SQLITE_OK {
|
||||||
eprintln(cstring_to_vstring(error_msg))
|
eprintln(unsafe { cstring_to_vstring(error_msg) })
|
||||||
C.sqlite3_free(error_msg)
|
C.sqlite3_free(error_msg)
|
||||||
}
|
}
|
||||||
C.sqlite3_close(db)
|
C.sqlite3_close(db)
|
||||||
|
|
|
@ -270,8 +270,8 @@ pub fn (s string) cstr() byteptr {
|
||||||
*/
|
*/
|
||||||
// cstring_to_vstring creates a copy of cstr and turns it into a v string.
|
// cstring_to_vstring creates a copy of cstr and turns it into a v string.
|
||||||
[unsafe]
|
[unsafe]
|
||||||
pub fn cstring_to_vstring(cstr &byte) string {
|
pub fn cstring_to_vstring(cstr &char) string {
|
||||||
return unsafe { tos_clone(cstr) }
|
return unsafe { tos_clone(&byte(cstr)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace_once replaces the first occurence of `rep` with the string passed in `with`.
|
// replace_once replaces the first occurence of `rep` with the string passed in `with`.
|
||||||
|
|
Loading…
Reference in New Issue