Unknown function C.sqlite3 and bad parameter or other API misuse

448 views Asked by At

If sqlite3_open_v2 is commentted, it gives an error: unknown function C.sqlite3 which is strange, why should the prototype affect db :=C.sqlite3(0)? But if it is commented then I get a bad parameter or other API misuse. What am I doing wrong?

import sqlite

//fn C.sqlite3_open_v2(charptr, &&C.sqlite3, int, charptr) int
struct C.sqlite3_stmt { }

fn main(){
    db := &C.sqlite3(0)
    stm := &C.sqlite3_stmt(0)
    db_path := ':memory:'
    query := 'select 1'

    C.sqlite3_open(db_path.str, &db)
    err := C.sqlite3_prepare_v2(&db, query.str, -1, &stm, C.NULL)
    if err != C.SQLITE_OK {
      C.puts(C.sqlite3_errstr(err))
    }
    C.sqlite3_close(db)
}
1

There are 1 answers

0
Major On

You're importing sqlite without really using it. In the sqlite module, there are wrapper functions to do what you want. This is how you would do it:

import sqlite

fn main() {
    db_path := ':memory:'
    db := sqlite.connect(db_path) or {
        panic(err)
    }
    query := 'select 1'
    val, code := db.exec(query) // declare code as 'mut' so it can be reused later
    assert code == 101 // 101 is the same as C.SQLITE_DONE
    println('$val') // prints the following:
    // [sqlite.Row{
    //     vals: ['1']
    // }]
    // No closing right now, not sure if it's in the works or needed
}