DatabaseExec
Script function

world.DatabaseExec

DOC_scripting Read about scripting

Type

Method

Summary

Executes SQL code against an SQLite database

Prototype

long DatabaseExec(BSTR Name, BSTR Sql);

DOC_data_types View list of data type meanings


Description

This executes SQL on a database previously opened with DatabaseOpen.

You need to supply the database id of an existing opened database, and the SQL statements to be executed. More than one statement may be supplied, separated by semicolons. If one statement causes an error, execution will be terminated. The most recent error message string (if any) can be obtained by calling DatabaseError.

After successful execution, DatabaseExec will return 0. For more explanation if zero is not returned, call DatabaseError.

You cannot call DatabaseExec if you have called DatabasePrepare but not DatabaseFinalize, as a statement is currently being processed.

DatabaseExec is not suitable for executing SQL code that queries the database for data (like a SELECT statement), as there is no provision for getting the results back. In that situation use DatabasePrepare / DatabaseStep / DatabaseFinalize.



Lua example

DatabaseOpen ("db", GetInfo (66) .. "mytestdb.sqlite", 6)

DatabaseExec ("db", [[
DROP TABLE IF EXISTS weapons;
CREATE TABLE weapons(
        weapon_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        name  TEXT NOT NULL,
        damage INT default 10,
        weight REAL
      );
      ]])
      
DatabaseClose ("db")  -- close it



Return value

0: Executed OK
-1 : Database id not found
-2 : Database not open
-3 : Already have prepared statement




See Also ...

Topics

DOC_database Database (SQLite)
DOC_lua_sqlite3 Lua SQLite (database) interface
DOC_scripting Scripting

Functions

FNC_DatabaseChanges DatabaseChanges (Returns a count of the changes to the database by the most recent SQL statement)
FNC_DatabaseClose DatabaseClose (Closes an SQLite database)
FNC_DatabaseColumnName DatabaseColumnName (Find the name of a specified column returned by an SQL statement)
FNC_DatabaseColumnNames DatabaseColumnNames (Return a table of all the columns returned by an SQL statement)
FNC_DatabaseColumns DatabaseColumns (Find how many columns will be returned by an SQL statement)
FNC_DatabaseColumnText DatabaseColumnText (Returns the contents of an SQL column, as text)
FNC_DatabaseColumnType DatabaseColumnType (Returns the type of data in an SQL column)
FNC_DatabaseColumnValue DatabaseColumnValue (Returns the contents of an SQL column, as text, float, integer, or null)
FNC_DatabaseColumnValues DatabaseColumnValues (Returns the contents of all the SQL columns after a step)
FNC_DatabaseError DatabaseError (Returns an English string describing the most recent SQL error)
FNC_DatabaseExec DatabaseExec (Executes SQL code against an SQLite database)
FNC_DatabaseFinalize DatabaseFinalize (Finalizes (wraps up) a previously-prepared SQL statement)
FNC_DatabaseInfo DatabaseInfo (Returns information about a database)
FNC_DatabaseLastInsertRowid DatabaseLastInsertRowid (Returns the most recently automatically allocated database key)
FNC_DatabaseList DatabaseList (Lists all databases)
FNC_DatabaseOpen DatabaseOpen (Opens an SQLite database)
FNC_DatabasePrepare DatabasePrepare (Prepares an SQL statement for execution)
FNC_DatabaseReset DatabaseReset (Resets a previously-prepared SQL statement to the start)
FNC_DatabaseStep DatabaseStep (Executes a previously-prepared SQL statement)
FNC_DatabaseTotalChanges DatabaseTotalChanges (Returns a count of the total changes to the database)

(Help topic: function=DatabaseExec)

DOC_contents Documentation contents page