SQLite ADO .NET和ExecuteScalar()

在SQLite ADO .Net(sqlite.phxsoftware.com)中,Execute Scalar()查询方法的文档读取:“执行命令并返回结果集第一行的第一列(如果存在),如果没有返回结果集,则返回null. ”.我创建了一个表:

create table users ( id integer primary key,name text )

并使用ExecuteScalar()执行队列:

select ( id ) from users where name = @name

但是,非常奇怪 – 我不能把返回值转换为’int’,只能’long’!为什么这样,’id’字段在数据库中被定义为’integer’,而不是’bigint’?

解决方法

这是因为SQLite“INTEGER PRIMARY KEYS”实际上不是整数,而是long(64位整数).从 documentation

Every row of every SQLite table has a
64-bit signed integer key that is
unique within the same table. This
integer is usually called the “rowid”.
The rowid is the actual key used in
the B-Tree that implements an SQLite
table. Rows are stored in rowid order.
The rowid value can be accessed using
one of the special names “ROWID”,
“OID”,or “ROWID“.

If a column is declared to be an
INTEGER PRIMARY KEY,then that column
is not a “real” database column but
instead becomes an alias for the
rowid. Unlike normal SQLite columns,
the rowid must be a non-NULL integer
value. The rowid is not able to hold
floating point values,strings,BLOBs,
or NULLs.

An INTEGER PRIMARY KEY column is an
alias for the 64-bit signed integer
rowid.

这是SQLite dynamic typing mechanism的副作用.

在实践中,要回答您的问题,您应该检索该值,并尽可能使用它,如果可能的话,仅使用转换运算符或转换类(如果值适合32位).因为它恰好是一个id,所以您不需要将其转换或转换为32位整数.

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能