sqlite_prepare

mIRC SQLite

sqlite_prepare
Prepares a SQL query to be executed later.
Syntax
$sqlite_prepare ( conn, query ) [ .file ]
Parameters
conn
The connection identifier.
query
The query to execute.
Properties
file
Optional. If specified the query parameter is treated as a filename instead, and that file will be executed as SQL.
Return Value
A positive, numeric statement identifier on success, or $null if there was an error.
Remarks
Prepared queries are efficient when you need to execute the same query many times with different parameters. This is because prepared query is only compiled once, and can then be executed without having to re-compile the query.
You can bind parameters in prepared queries, for more information about prepared statements and parameter binding, see Prepared Statements.
Just like ordinary queries, prepared queries are executed with $sqlite_exec or $sqlite_query, see example below.

If $null is returned you can determine the exact reason for the error by checking the value of %sqlite_errstr.
For more information about error handling, see Handling Errors.

$sqlite_prepare can only prepare a single query. Extra queries seperated by a semi-colon are ignored, only the first one is prepared.
To see guidelines for writing SQL queries with mIRC SQLite, see Writing Queries.
Example
; Inserts data into a table two times with different parameters
var %sql = INSERT INTO table VALUES (?, :test)
var %stmt = $sqlite_prepare(%db, %sql)
if (%stmt) {
  echo -a Query prepared successfully.

  ; Binds Hello as first parameter, and World as second parameter and inserts the row
  sqlite_bind_value %stmt 1 Hello
  sqlite_bind_value %stmt :test World
  sqlite_exec %stmt

  ; Binds NULL as first parameter, and 100 as second parameter and inserts the row
  sqlite_bind_null %stmt 1
  sqlite_bind_value %stmt :test 100
  sqlite_exec %stmt

  ; Binds 'This is a test' as first parameter, and uses the previously bound parameter for second parameter
  noop $sqlite_exec(%stmt, This is a test)

  sqlite_free %stmt
}
else {
  echo -a Error preparing query: %sqlite_errstr
}