开发者

Sqlite WHERE clause and Delphi XE longword values

In Delphi XE, I am storing crc32 hash of a string in an SQlite database, in a column declared as INTEGER. My understanding is that SQlite does not distinguish among integer types: int, int64, signed and unsigned, they're all the same as far as the database is concerned. However, when I store a value declared as longword in Delphi, a WHERE clause fails to match on that value later.

My insert statement (trimmed down here) is:

INSERT INTO main VALUES (id, crc) (?, ?);

The longword value gets bound to the second parameter and all goes well. But when I do

SELECT id FROM开发者_高级运维 main WHERE crc = ?;

the query returns no results.

Viewing the database in SQLiteSpy, the longword values are displayed as negative integers. If I execute the above SELECT with the negative value copy-and-pasted from that display, the query returns the expected record.

It would seem that when I bind a longword value to the INSERT statement, SQLIte does something else then when I bind the same longword value to the SELECT statement. Casting the value to integer (in Delphi code, not in SQL) fixes the problem, but it should not be necessary, and it'll be easy to forget to cast in other places. Is there a better solution? Is SQLite behaving correctly?


The problem is not in SQLite, but in the way you're binding your parameters to the SQLite engine.

Depending on the SQlite framework you're using, you must explicitely bound an Int64 to your statement:

INSERT INTO main VALUES (id, crc) (?, ?);

For example, using our framework, with a CRC declared as a Cardinal, you must use this code (and NOT any integer(CRC)):

sqlite3_check(RequestDB,sqlite3_bind_Int64(Request,2,Int64(CRC)));

If the above code doesn't work, this will always work:

var CRC64: Int64;
    CRC64Rec: TInt64Rec absolute CRC64;
begin
  CRC64Rec.Lo := CRC;
  CRC64Rec.Hi := 0;
  sqlite3_check(RequestDB,sqlite3_bind_Int64(Request,2,CRC64));

In all cases, without showing your own code about binding parameters, it's difficult to know what's wrong in your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜