SQLite ADO .NET和ExecuteScalar()

前端之家收集整理的这篇文章主要介绍了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相关文章