加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLite ADO .NET和ExecuteScalar()

发布时间:2020-12-12 18:51:44 所属栏目:百科 来源:网络整理
导读:在SQLite ADO .Net(sqlite.phxsoftware.com)中,Execute Scalar()查询方法的文档读取:“执行命令并返回结果集第一行的第一列(如果存在),如果没有返回结果集,则返回null. ”.我创建了一个表: create table users ( id integer primary key,name text ) 并使
在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位整数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读