sqlite_prepare_v2

Forum related to SQLite

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
rayanAyar
Fresh Boarder
Fresh Boarder
Posts: 13
Joined: 02.07.2010, 04:26

sqlite_prepare_v2

Post by rayanAyar »

Is there any reason to use SQLite API function "sqlite_prepare" instead of "sqlite_prepare_v2" ?
Both functions are listed in code:
http://sourceforge.net/p/zeoslib/code-0 ... r.pas#l336
But used only first:
http://sourceforge.net/p/zeoslib/code-0 ... r.pas#l984

According to the SQLite documentation:
sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are recommended for all new programs
Main advantage of "_v2" is that "sqlite3_prepare_v2" will try to re-compile SQL statement if schema changes.

I migrated to Zeos 7.1.2 from 7.0.3. And my software begin to crash with error "database schema has changed".
SQLite FAQ says:
The easiest way to deal with SQLITE_SCHEMA errors is to always use sqlite3_prepare_v2() instead of sqlite3_prepare()
So I changed both calls of sqlite3_prepare to sqlite3_prepare_v2:

Code: Select all

{ Prepared statmenet api }
function TZSQLiteBaseDriver.Prepare(db: Psqlite; const zSql: PAnsiChar; nBytes: Integer;
  out ppStmt: Psqlite3_stmt; pzTail: PPAnsichar): Integer;
begin
  Result := SQLite_API.sqlite_prepare_v2(db, zSql, nBytes, ppStmt, pzTail);
end;

function TZSQLiteBaseDriver.Prepare_v2(db: Psqlite; const zSql: PAnsiChar; nBytes: Integer;
  out ppStmt: Psqlite3_stmt; pzTail: PPAnsichar): Integer;
begin
  Result := SQLite_API.sqlite_prepare_v2(db, zSql, nBytes, ppStmt, pzTail);
end;
And it works well for me.
User avatar
EgonHugeist
Zeos Project Manager
Zeos Project Manager
Posts: 1936
Joined: 31.03.2011, 22:38

Re: sqlite_prepare_v2

Post by EgonHugeist »

Thank you for your details.

Just commited your changes to \testing-7.2 (SVN)

Patch done R3060

What about Open_V2? Do you see some advantages if we would support the different modes?
Best regards, Michael

You want to help? http://zeoslib.sourceforge.net/viewtopic.php?f=4&t=3671
You found a (possible) bug? Use the new bugtracker dude! http://sourceforge.net/p/zeoslib/tickets/

Image
rayanAyar
Fresh Boarder
Fresh Boarder
Posts: 13
Joined: 02.07.2010, 04:26

Re: sqlite_prepare_v2

Post by rayanAyar »

EgonHugeist wrote: Just commited your changes to \testing-7.2 (SVN)
Patch done R3060
If patching "procedure TZSQLiteCAPIPreparedStatement.Prepare" then probably need to patch here:
http://sourceforge.net/p/zeoslib/code-0 ... r.pas#l990

Currently there is a typo:
function TZSQLiteBaseDriver.Prepare_v2(db: Psqlite; const zSql: PAnsiChar; nBytes: Integer;
out ppStmt: Psqlite3_stmt; pzTail: PPAnsichar): Integer;
begin
Result := SQLite_API.sqlite_prepare(db, zSql, nBytes, ppStmt, pzTail);
end;
EgonHugeist wrote: What about Open_V2? Do you see some advantages if we would support the different modes?
Didn't try Open_V2 because there is no need for me.
User avatar
EgonHugeist
Zeos Project Manager
Zeos Project Manager
Posts: 1936
Joined: 31.03.2011, 22:38

Re: sqlite_prepare_v2

Post by EgonHugeist »

If patching "procedure TZSQLiteCAPIPreparedStatement.Prepare" then probably need to patch here:
http://sourceforge.net/p/zeoslib/code-0 ... r.pas#l990
Of course... But all development starts in the \testing-X branches. So the patch goes to current \testing-7.2 first. Than (if tested) the changes will be merged to \trunk after a periode of a week or two. If such a patch is signed with a minior version like "*7.1*" the patch goes to the current stable release too.
Just be patiant. Two weeks later you'll have the patch in \trunk too.
Best regards, Michael

You want to help? http://zeoslib.sourceforge.net/viewtopic.php?f=4&t=3671
You found a (possible) bug? Use the new bugtracker dude! http://sourceforge.net/p/zeoslib/tickets/

Image
rayanAyar
Fresh Boarder
Fresh Boarder
Posts: 13
Joined: 02.07.2010, 04:26

Re: sqlite_prepare_v2

Post by rayanAyar »

I meant that patching "procedure TZSQLiteCAPIPreparedStatement.Prepare" (patch R3060) does nothing.
Because there is a typo in "TZSQLiteBaseDriver.Prepare_v2":
function TZSQLiteBaseDriver.Prepare(db: Psqlite; const zSql: PAnsiChar; nBytes: Integer;
out ppStmt: Psqlite3_stmt; pzTail: PPAnsichar): Integer;
begin
Result := SQLite_API.sqlite_prepare(db, zSql, nBytes, ppStmt, pzTail);
end;

function TZSQLiteBaseDriver.Prepare_v2(db: Psqlite; const zSql: PAnsiChar; nBytes: Integer;
out ppStmt: Psqlite3_stmt; pzTail: PPAnsichar): Integer;
begin
Result := SQLite_API.sqlite_prepare(db, zSql, nBytes, ppStmt, pzTail); // prepare instead of prepare_v2
end;

function TZSQLiteBaseDriver.Prepare16(db: Psqlite; const zSql: PWideChar; nBytes: Integer;
out ppStmt: Psqlite3_stmt; pzTail: ZPPWideChar): Integer;
begin
Result := SQLite_API.sqlite_prepare16(db, zSql, nBytes, ppStmt, pzTail);
end;

function TZSQLiteBaseDriver.Prepare16_v2(db: Psqlite; const zSql: PWideChar; nBytes: Integer;
out ppStmt: Psqlite3_stmt; pzTail: ZPPWideChar): Integer;
begin
Result := SQLite_API.sqlite_prepare16_v2(db, zSql, nBytes, ppStmt, pzTail);
end;
So changing FPlainDriver.Prepare to FPlainDriver.Prepare_v2 is not enough.
User avatar
EgonHugeist
Zeos Project Manager
Zeos Project Manager
Posts: 1936
Joined: 31.03.2011, 22:38

Re: sqlite_prepare_v2

Post by EgonHugeist »

So changing FPlainDriver.Prepare to FPlainDriver.Prepare_v2 is not enough.
Indeed you're right again. Man reading post more carefully should help... :o

Patch done R3062 \testing-7.2 (SVN)

Confirmed?
Best regards, Michael

You want to help? http://zeoslib.sourceforge.net/viewtopic.php?f=4&t=3671
You found a (possible) bug? Use the new bugtracker dude! http://sourceforge.net/p/zeoslib/tickets/

Image
rayanAyar
Fresh Boarder
Fresh Boarder
Posts: 13
Joined: 02.07.2010, 04:26

Re: sqlite_prepare_v2

Post by rayanAyar »

Yes, that works for me (tested R3060 &R3062 on Zeos 7.1.2).
Post Reply