DatabasePrepare
Script function

world.DatabasePrepare

DOC_scripting Read about scripting

Type

Method

Summary

Prepares an SQL statement for execution

Prototype

long DatabasePrepare(BSTR Name, BSTR Sql);

DOC_data_types View list of data type meanings


Description

Prepares (compiles) an SQL statement, ready to be executed with DatabaseStep.

You need to supply the database id of an existing opened database, and the SQL statement to be executed. Only one statement should be supplied. If multiple statements are supplied, only the first will be used. (eg. "drop table a; drop table b" - in this case only the first "drop table" would be executed).

If there is a syntax error in your SQL, that will be reported by a non-zero return code from DatabasePrepare. The most recent error message string (if any) can be obtained by calling DatabaseError.

Note that to include single quotes inside a string literal, in your SQL statement they must be doubled. For example:

INSERT INTO weapons (name, damage) VALUES ('Nick''s sword', 42)

Only one statement can be active at once, so after doing a DatabasePrepare you must do DatabaseFinalize before doing another DatabasePrepare. The exception is that if DatabasePrepare returns an error, then you do not need to do DatabaseFinalize because there is no active statement.

After successfully doing DatabasePrepare, you then need to call DatabaseStep for each row in the query. This is the execution phase. For SQL statements that return multiple rows of data, you need to do DatabaseStep once for each row. For SQL statements that just do one thing (like INSERT INTO) then you only need to call DatabaseStep once.

Also see DatabaseExec if you only need to execute SQL statements (like INSERT INTO) which do not return any results.



Lua example

-- insert a record
DatabasePrepare ("db", "INSERT INTO weapons (name, damage) VALUES ('sword', 42)")  --> returns 0 (SQLITE_OK)
DatabaseStep ("db")   -- returns 101 (SQLITE_DONE)
DatabaseFinalize ("db")  -- returns 0 (SQLITE_OK)


-- see the DatabaseStep entry for a more elaborate example.



Return value

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

Otherwise an SQLite return code. For example:

1: SQL error or missing database
5 : The database file is locked




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_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=DatabasePrepare)

DOC_contents Documentation contents page