world.DatabaseStep

MUSHclient script function (Method) — introduced in version 4.40

Executes a previously-prepared SQL statement

Prototype

long DatabaseStep(BSTR DbName);

Data type meanings

Description

Executes an SQL statement over one row over the database. In the case of a SELECT statement, one row is returned for each call to DatabaseStep.

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.

DatabaseStep will return SQLITE_ROW (100) if it has returned a row of data, otherwise it will return SQLITE_DONE (101) if it has reached the end of the query. You could make a loop that continues to call DatabaseStep until it no longer returns 100, in order to process every row in the query.

If you want to reprocess the SQL query from the start for some reason, you can call DatabaseReset and the query restarts at the beginning (you would then need to call DatabaseStep again to re-obtain the first row).

Lua example

require "tprint"

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

rc = 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
      );
      ]])
      

-- put some data into the database
DatabaseExec ("db", 
  [[
  INSERT INTO weapons (name, damage) VALUES ('sword', 42);
  INSERT INTO weapons (name, damage) VALUES ('mace', 55);
  INSERT INTO weapons (name, damage) VALUES ('staff', 35);
  ]])

-- prepare a query
DatabasePrepare ("db", "SELECT * from weapons ORDER BY name")

-- find the column names
names = DatabaseColumnNames ("db")
tprint (names)

-- execute to get the first row
rc = DatabaseStep ("db")  -- read first row

-- now loop, displaying each row, and getting the next one
while rc == sqlite3.ROW do
  
  print ("")
  values = DatabaseColumnValues ("db")
  tprint (values)

  rc = DatabaseStep ("db")  -- read next row

end -- while loop

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it

Lua notes

The return codes are available in the sqlite3 table in Lua, as follows:

sqlite3.OK = 0
sqlite3.INTEGER = 1
sqlite3.INTERNAL = 2
sqlite3.PERM = 3
sqlite3.ABORT = 4
sqlite3.BUSY = 5
sqlite3.LOCKED = 6
sqlite3.NOMEM = 7
sqlite3.READONLY = 8
sqlite3.INTERRUPT = 9
sqlite3.IOERR = 10
sqlite3.CORRUPT = 11
sqlite3.NOTFOUND = 12
sqlite3.FULL = 13
sqlite3.CANTOPEN = 14
sqlite3.PROTOCOL = 15
sqlite3.EMPTY = 16
sqlite3.SCHEMA = 17
sqlite3.TOOBIG = 18
sqlite3.CONSTRAINT = 19
sqlite3.MISMATCH = 20
sqlite3.MISUSE = 21
sqlite3.NOLFS = 22
sqlite3.FORMAT = 24
sqlite3.RANGE = 25
sqlite3.NOTADB = 26
sqlite3.ROW = 100
sqlite3.DONE = 101

Return value

100: Another row is ready.
101: Completed OK, no further rows.

-1 : Database id not found
-2 : Database not open
-4 : Do not have prepared statement

Otherwise an SQLite return code. For example:

1: SQL error or missing database
5 : The database file is locked
21 : Library used incorrectly

Related topic

Database (SQLite)

See also

FunctionDescription
DatabaseColumnNameFind the name of a specified column returned by an SQL statement
DatabaseColumnNamesReturn a table of all the columns returned by an SQL statement
DatabaseColumnTextReturns the contents of an SQL column, as text
DatabaseColumnTypeReturns the type of data in an SQL column
DatabaseColumnValueReturns the contents of an SQL column, as text, float, integer, or null
DatabaseColumnValuesReturns the contents of all the SQL columns after a step
DatabaseColumnsFind how many columns will be returned by an SQL statement
DatabaseErrorReturns an English string describing the most recent SQL error
DatabaseExecExecutes SQL code against an SQLite database
DatabaseFinalizeFinalizes (wraps up) a previously-prepared SQL statement
DatabaseLastInsertRowidReturns the most recently automatically allocated database key
DatabaseOpenOpens an SQLite database
DatabasePreparePrepares an SQL statement for execution
DatabaseResetResets a previously-prepared SQL statement to the start