Register forum user name Search FAQ

DatabaseStep

Script function

world.DatabaseStep

Read about scripting

Type

Method

Summary

Executes a previously-prepared SQL statement

Prototype

long DatabaseStep(BSTR DbName);

View list of 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).


Available in MUSHclient version 4.40 onwards.



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


See Also ...

Topics

Database (SQLite)
Lua SQLite (database) interface
Scripting

Functions

(DatabaseChanges) Returns a count of the changes to the database by the most recent SQL statement
(DatabaseClose) Closes an SQLite database
(DatabaseColumnName) Find the name of a specified column returned by an SQL statement
(DatabaseColumnNames) Return a table of all the columns returned by an SQL statement
(DatabaseColumns) Find how many columns will be returned by an SQL statement
(DatabaseColumnText) Returns the contents of an SQL column, as text
(DatabaseColumnType) Returns the type of data in an SQL column
(DatabaseColumnValue) Returns the contents of an SQL column, as text, float, integer, or null
(DatabaseColumnValues) Returns the contents of all the SQL columns after a step
(DatabaseError) Returns an English string describing the most recent SQL error
(DatabaseExec) Executes SQL code against an SQLite database
(DatabaseFinalize) Finalizes (wraps up) a previously-prepared SQL statement
(DatabaseGetField) Returns a single field from an SQL database
(DatabaseInfo) Returns information about a database
(DatabaseLastInsertRowid) Returns the most recently automatically allocated database key
(DatabaseList) Lists all databases
(DatabaseOpen) Opens an SQLite database
(DatabasePrepare) Prepares an SQL statement for execution
(DatabaseReset) Resets a previously-prepared SQL statement to the start
(DatabaseTotalChanges) Returns a count of the total changes to the database

(Help topic: function=DatabaseStep)

Documentation contents page


Search ...

Enter a search string to find matching documentation.

Search for:   

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.