Script function
world.DatabaseGetField
Read about scripting
Type
Method
Summary
Returns a single field from an SQL database
Prototype
VARIANT DatabaseGetField(BSTR Name, BSTR Sql);
View list of data type meanings
Description
This executes SQL on a database previously opened with DatabaseOpen.
It lets you obtain a single value more easily than doing a DatabasePrepare / DatabaseStep / DatabaseColumnValue / DatabaseFinalize in the cases where you just want to establish a single thing (eg. the count of spells in a table).
You need to supply the database id of an existing opened database, and the SQL statement to be executed.
You cannot call DatabaseGetField if you have called DatabasePrepare but not DatabaseFinalize, as a statement is currently being processed.
The function effectively does this:
DatabasePrepare (Name, Sql)
If unsuccessful, exit with null variant.
Otherwise:
DatabaseStep (Name)
If successful, and a row was returned, then:
result = DatabaseColumnValue (Name, 1)
DatabaseFinalize (Name)
If successful, then the value for the first column of the first row is the return value, otherwise the null variant (nil in Lua).
Available in MUSHclient version 4.65 onwards.
Lua example
DatabaseOpen ("db", GetInfo (82), 1)
value = DatabaseGetField ("db",
"SELECT value FROM control WHERE name = 'Version' ")
DatabaseClose ("db")
Lua notes
Returns nil on error.
Return value
The contents of the column, as a string, long, double or empty variant.
Returns the null variant if there is an error, such as invalid database id, no current row, etc.
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
(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
(DatabaseStep) Executes a previously-prepared SQL statement
(DatabaseTotalChanges) Returns a count of the total changes to the database
(Help topic: function=DatabaseGetField)