# sqlite3_exec

Definition: | include pSQLite.e integer res = sqlite3_exec(sqlite3 db, string cmd, integer rid=0, atom user_data=NULL)
Description: | Zero or more SQL statements specified in the second parameter are compiled and executed. db: an open database, a result from sqlite3_open(). cmd: the SQL to be executed. rid: the routine_id of a function, see example 2 below. user_data: will be passed as the third parameter to rid. The return value is SQLITE_OK if there are no errors and some other return code if there is an error. The particular return value depends on the type of error. If the SQL statement could not be executed because a database file is locked or busy, then this function returns SQLITE_BUSY. If rid is specified and the SQL command is a query, it will call that function with details of each row retrieved. Should it return a non-zero value, then the sqlite3_exec() call in turn returns SQLITE_ABORT immediately, without processing any more rows.
pwa/p2js: | Not supported
Example 1: | Copy
    include pSQLite.e
    sqlite3 db = sqlite3_open("log.sqlite")
    integer res = sqlite3_exec(db,"CREATE TABLE test (id,desc);")
    res = sqlite3_exec(db,"INSERT INTO test VALUES (1,'one');")
    sqlite3_close(db)
Example 2: | Copy
    include pSQLite.e
    sqlite3 db = sqlite3_open("test.sqlite")

    procedure db_exec(string cmd, bool fatal=true)
        integer res = sqlite3_exec(db,cmd)
        if fatal and res!=SQLITE_OK then ?9/0 end if
    end procedure

    function process_one_row(sequence data, cols, atom user_data)
        ?{"process_row",data,cols,user_data}
        return 0    -- (any other value raises SQLITE_ABORT)
    end function
    constant r_row = routine_id("process_one_row")

    db_exec("BEGIN TRANSACTION;")
    db_exec("DROP TABLE test;",fatal:=false)
    db_exec("CREATE TABLE test(id, desc);")
    db_exec("INSERT INTO test VALUES(1, 'one');")
    db_exec("INSERT INTO test VALUES(2, 'two');")
    integer res = sqlite3_exec(db,"SELECT * FROM test;",r_row,34)
    if res!=SQLITE_OK then
        if res!=SQLITE_ABORT then ?9/0 end if
        ?"SQLITE_ABORT"
    end if
    db_exec("DROP TABLE test;")
    db_exec("END TRANSACTION;")
    sqlite3_close(db)

    -- output:
    --  {"process_row",{"1","one"},{"id","desc"},34}
    --  {"process_row",{"2","two"},{"id","desc"},34}
Of course the 34 is a meaningless value for user_data in this example, but it could be a pointer, or an index, or something similar.
See Also: | sqlite3_open