On Wed, 27 Oct 2004 15:11:09 -0500, Tom <[EMAIL PROTECTED]> wrote: > Vladimir, > Thank you for your responses. One short followup: > > >> A. what happens to the zSql? Is it copied or it is assumed that this > >> is a static text? Suppose I call sqlite3_prepare in one place and > >> immediately release dynamically allocated text. sqlite3_prepare parses > >> only the first statement. Then I call sqlite3_step as many times as > >> needed. Will it crash when trying to parse next statements in next > >> steps? > > zSql is parsed and turned into an internal VM bytecode for later > > execution; you're under no obligation to keep zSql around after > > prepare returns. > This would suggest that the whole string is parsed even if it contains > more than one statement. > According to documentation on sqlite3_prepare(): > "This routine only compiles the first statement in zSql, so *pzTail is > left pointing to what remains uncompiled."
Sorry, I was unclear; _prepare will return a stmt corresponding to the bytecode for the first SQL statement in zSql. So, if you want to compile multiple statements, you'll have to keep zSql around and use pzTail appropriately. > >> B. Is pzTail of any use? I can see that inside the library code it is > >> checked against NULL before assigning the output text pointer but the > >> documentation does not state explicitly that this parameter is > >> optional. I don't want to rely on internal implementation (which may > >> change in future release) when the docs don't say it is optional. It > >> would be nice to clarify this. > > > > It's useful if you're reading SQL from the user, and you get, say, two > > INSERT commands in one string. pzTail will let you call > > prepare/step/etc. multiple times to evaluate all the sql. It would be > > good to explicitly state that it may be NULL. > I don't understand something. Suppose you have 2 statements in the SQL > string. Do you have to call sqlite3_prepare() 2 times? I was under > impression that sqlite3_step() will do it for me if I call it multiple > times. I tried to find some sample code via google and I did not see a > single example of calling sqlite3_prepare() multiple times for one SQL > string. If you have two SQL statements in one string, you'll need to create two sqlite3_stmt's by calling prepare twice, with the second one using the pzTail result of the first (to point at the second SQL statement). To execute, you'll have to step both stmt's (step the first until it returns DONE, then step the second until it returns DONE). sqlite3_step only applies to one sqlite3_stmt, which in turn contains only one compiled SQL expression -- step is there to step between result rows, not to step between statements. One somewhat involved example is the implementation of sqlite3_exec in legacy.c in the sqlite3 sources -- it prepares and executes all statements in a string. - Vlad