[Home] [Downloads] [Search] [Help/forum]

db:prepare

Summary

Compiles an SQL statement

Prototype

db:prepare(sql)


Description

This function compiles the SQL statement in string sql into an internal representation and returns this as userdata. The returned object should be used for all further method calls in connection with this specific SQL statement.

CALLBACK CONTEXTS

A callback context is available as a parameter inside the callback functions db:create_aggregate(), db:create_function() and db:prepare(). It can be used to get further information about the state of a query.

The various context functions are:

context:aggregate_count
context:get_aggregate_data
context:result
context:result_blob
context:result_error
context:result_int
context:result_null
context:result_number
context:result_text
context:set_aggregate_data
context:user_data

Example


require "tprint"

-- make in-memory database
db = sqlite3.open_memory()

-- make a table for testing
db:exec  [[
          CREATE TABLE mobs (name, class, hp);
          INSERT INTO mobs VALUES("Naga", "mage", 666);
          INSERT INTO mobs VALUES("Wolf", "beast", 42);
          INSERT INTO mobs VALUES("Guard", "warrior", 100);
       ]]

-- prepare a SELECT statement
local stmt = db:prepare ("SELECT * FROM mobs")

-- loop until we get everything
while true do

  local result = stmt:step ()

  -- exit loop if done
  if result == sqlite3.DONE then
    break
  end -- if done

  -- should have ROW result
  assert (result == sqlite3.ROW, "Row not found")

  -- get all values into a table
  local row = stmt:get_named_values ()

  -- display them
  print (string.rep ("-", 20))
  tprint (row)
  
end -- while

-- done with this statement
stmt:finalize ()

-- finished with database
db:close ()


See Also ...

Lua functions

context:aggregate_count - Returns the number of calls to the aggregate step function.
context:get_aggregate_data - Returns the user-definable data field for callback functions.
context:result - This function sets the result of a callback function to res.
context:result_blob - This function sets the result of a callback function to the binary string in blob.
context:result_error - This function sets the result of a callback function to the error value in err.
context:result_int - This function sets the result of a callback function to the integer value in number
context:result_null - This function sets the result of a callback function to nil.
context:result_number - This function sets the result of a callback function to the value number.
context:result_text - This function sets the result of a callback function to the string in str.
context:set_aggregate_data - Set the user-definable data field for callback functions to udata.
context:user_data - Returns the userdata parameter given in the call to install the callback function.
db:busy_handler - Sets or removes a busy handler for a database
db:busy_timeout - Sets a busy handler
db:changes - Returns number of changes by the most recent SQL statement
db:close - Closes a database
db:close_vm - Finalizes all statements that have not been explicitly finalized
db:create_aggregate - Creates an aggregate callback function
db:create_collation - Creates a collation callback
db:create_function - Creates a callback function
db:errcode - Returns the most recent result code
db:errmsg - Returns the most recent error message
db:exec - Executes SQL statements
db:interrupt - Interrupts any pending operation
db:isopen - Tests if the database is open
db:last_insert_rowid - Returns the key of the most recent insert into the database
db:nrows - Creates an iterator that returns a table of rows from a SELECT - keyed by name
db:progress_handler - Installs a progress callback function
db:rows - Creates an iterator that returns a table of rows from a SELECT - numerically keyed
db:total_changes - Returns total number of changes
db:trace - Installs debug trace callback
db:urows - Creates an iterator that returns the rows from a SELECT
sqlite3.complete - Checks for a complete SQL statement
sqlite3.open - Opens or creates a database
sqlite3.open_memory - Opens a database in memory
sqlite3.version - Returns the SQLite version
stmt:bind - Binds a value to a parameter
stmt:bind_blob - Binds a blob to a parameter
stmt:bind_names - Binds values in a table to parameters
stmt:bind_parameter_count - Returns the largest parameter index
stmt:bind_parameter_name - Returns the name of the n'th paremeter
stmt:bind_values - Binds values to parameters
stmt:columns - Returns number of columns in the result set
stmt:finalize - Frees a prepared statement
stmt:get_name - Returns the name of a column in a result set
stmt:get_named_types - Returns a table of names and types for the columns in a result set
stmt:get_named_values - Returns a table of names and values for the columns in a result set
stmt:get_names - Returns a table of the names of all columns in a result set
stmt:get_type - Returns the type of a column in a result set
stmt:get_types - Returns a table of the types of all columns in a result set
stmt:get_unames - Returns a list of names of the columns in a result set
stmt:get_utypes - Returns a list of types of the columns in a result set
stmt:get_uvalues - Returns a list of values of the columns in a result set
stmt:get_value - Returns the value of a column in a result set
stmt:get_values - Returns a table of the values of all columns in a result set
stmt:isopen - Checks if the statement is open
stmt:nrows - Creates an iterator that returns a table of rows from a SELECT - keyed by name
stmt:reset - Resets an SQL statement, so it can be executed again
stmt:rows - Creates an iterator that returns a table of rows from a SELECT - numerically keyed
stmt:step - Executes a previously-prepared SQL statement
stmt:urows - Creates an iterator that returns the rows from a SELECT

Topics

Database (SQLite)
Lua script extensions
Lua SQLite (database) interface
Lua syntax
Scripting
Scripting callbacks - plugins

(Help topic: lua=db:prepare)

Documentation contents page


Search ...

Enter a search string to find matching documentation.

Search for:   

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]