Skip to content

camlsql.prepare method

David Lidström edited this page Nov 11, 2017 · 1 revision

Home » camlsql.prepare Method

Prepare is the bread and butter of camlsql. This is basically always your starting point.

Prepare your query and bind parameters

 CamlSqlQuery camlsql.prepare ( sql_query[, parameters[] ] )

Parameter binding

Parameters can be bound in two ways - by name or by parameter index.

Bind by parameter index

Pass an array as parameter to bind by index

 CamlSqlQuery camlsql.prepare(sql_query, object[] );

For simple queries you can use ? as a macro in your SQL query to indicate the use of a parameter. The first ? will use the first parameter from the parameter array, the second ? will use the second and so on.

camlsql.prepare("SELECT * FROM [Pages] WHERE Created < ? AND Title LIKE ?", [
 camlsql.today(), // This is the "Created < ?" parameter
 "%fun%"          // This is the "Title LIKE ?" parameter
]).getXml();

Notice the getXml method. This is a method of the returned CamlSqlQuery object.

Named parameters

Pass an object as parameter to bind by index

 camlsql.prepare(sql_query, object);

If you have a lot of parameters, or need to use the same value many times in your query, then the query is easier to read if you use named queries.

camlsql.prepare("SELECT * FROM [Pages] WHERE Title LIKE @query OR Description LIKE @query OR Field3 LIKE @query", 
 {"@query" : "%cheese%"}
).getXml()

Return value

The prepare method will return a CamlSqlQuery object from which you can execute the query immediatly, or simply retreive the generated CAML Xml.