SQLite3 中怎么查找最小化可用设计可用的空缺ID

Asynchronous, non-blocking
bindings for .
Supported platforms
The sqlite3 module works with Node.js v0.10.x, v0.12.x, v4.x, v5.x, v6.x and v7.x.
Binaries for most Node versions and platforms are provided by default via .
The sqlite3 module also works with
if node-webkit contains a supported version of Node.js engine.
is also supported.
Note: the module must be
before use.
var sqlite3 = require('sqlite3').verbose();var db = new sqlite3.Database(':memory:'); db.serialize(function() {
db.run(&CREATE TABLE lorem (info TEXT)&);
var stmt = db.prepare(&INSERT INTO lorem VALUES (?)&);
for (var i = 0; i & 10; i++) {
stmt.run(&Ipsum & + i);
stmt.finalize();
db.each(&SELECT rowid AS id, info FROM lorem&, function(err, row) {
console.log(row.id + &: & + row.info);
});}); db.close();Features
Straightforward query and parameter binding interface
Full Buffer/Blob support
Big test suite
Written in modern C++ and tested for memory leaks
Bundles Sqlite3 3.15.0 as a fallback if the installing system doesn't include SQLite
in the wiki.
Installing
You can use
to download and install:
The latest sqlite3 package: npm install sqlite3
GitHub's master branch: npm install https://github.com/mapbox/node-sqlite3/tarball/master
The module uses
to download a pre-compiled binary for your platform, if it exists. Otherwise, it uses node-gyp to build the extension.
It is also possible to make your own build of sqlite3 from its source instead of its npm package ().
It is possible to use the installed package in
instead of the vanilla Node.js. See
for details.
Source install
To skip searching for pre-compiled binaries, and force a build from source, use
npm install --build-from-source
The sqlite3 module depends only on libsqlite3. However, by default, an internal/bundled copy of sqlite will be built and statically linked, so an externally installed sqlite3 is not required.
If you wish to install against an external sqlite then you need to pass the --sqlite argument to npm wrapper:
npm install --build-from-source --sqlite=/usr/local
If building against an external sqlite3 make sure to have the development headers available. Mac OS X ships with these by default. If you don't have them installed, install the -dev package with your package manager, e.g. apt-get install libsqlite3-dev for Debian/Ubuntu. Make sure that you have at least libsqlite3 &= 3.6.
Note, if building against homebrew-installed sqlite on OS X you can do:
npm install --build-from-source --sqlite=/usr/local/opt/sqlite/
Building for node-webkit
Because of ABI differences, sqlite3 must be built in a custom to be used with .
To build node-sqlite3 for node-webkit:
globally: npm install nw-gyp -g (unless already installed)
Build the module with the custom flags of --runtime, --target_arch, and --target:
NODE_WEBKIT_VERSION=&0.8.6&
see latest version at https://github.com/rogerwang/node-webkit#downloads npm install sqlite3 --build-from-source --runtime=node-webkit --target_arch=ia32 --target=$(NODE_WEBKIT_VERSION)
This command internally calls out to
which itself calls out to
when the --runtime=node-webkit option is passed.
You can also run this command from within a node-sqlite3 checkout:
npm install --build-from-source --runtime=node-webkit --target_arch=ia32 --target=$(NODE_WEBKIT_VERSION)
Remember the following:
You must provide the right --target_arch flag. ia32 is needed to target 32bit node-webkit builds, while x64 will target 64bit node-webkit builds (if available for your platform).
After the sqlite3 package is built for node-webkit it cannot run in the vanilla Node.js (and vice versa).
For example, npm test of the node-webkit's package would fail.
Visit the “” article in the node-webkit's wiki for more details.
Building for sqlcipher
For instructions for building sqlcipher see
To run node-sqlite3 against sqlcipher you need to compile from source by passing build options like:
npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=/usr/
node -e 'require(&sqlite3&)'
If your sqlcipher is installed in a custom location (if you compiled and installed it yourself),
you'll also need to to set some environment variables:
On OS X with Homebrew
Set the location where brew installed it:
export LDFLAGS=&-L`brew --prefix`/opt/sqlcipher/lib&
export CPPFLAGS=&-I`brew --prefix`/opt/sqlcipher/include&
npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=`brew --prefix`
node -e 'require(&sqlite3&)'
On most Linuxes (including Raspberry Pi)
Set the location where make installed it:
export LDFLAGS=&-L/usr/local/lib&
export CPPFLAGS=&-I/usr/local/include -I/usr/local/include/sqlcipher&
export CXXFLAGS=&$CPPFLAGS&
npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=/usr/local --verbose
node -e 'require(&sqlite3&)'
Custom builds and Electron
Running sqlite3 through
does not preserve the sqlcipher extension, so some additional flags are needed to make this build Electron compatible. Your npm install sqlite3 --build-from-source command needs these additional flags (be sure to replace the target version with the current Electron version you are working with):
--runtime=electron --target=1.7.6 --dist-url=https://atom.io/download/electron
In the case of MacOS with Homebrew, the command should look like the following:
npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=`brew --prefix` --runtime=electron --target=1.7.6 --dist-url=https://atom.io/download/electron
is required to run unit tests.
In sqlite3's directory (where its package.json resides) run the following:
npm install mocha
Contributors
Acknowledgments
Thanks to ,
for their SQLite bindings for node, and to mraleph on Freenode's #v8 for answering questions.
Development of this module is sponsored by .
node-sqlite3 is .
T19:37:33.180Z
is the latest
of 70 releases
Collaborators
downloads in the last day
downloads in the last week
downloads in the last month
Have an issue?
Try it out
Dependencies (2)16:41 提问
ios中sql查询及取数字最大值
表名:inventory
字段sheetCode ,字段sheetCode的内容分别是552, 275, cs587, be78g, 9h87, 87gb, 998, 276
如上:请问怎么用sql语句查询纯数字最大值,即查询结果998
按赞数排序
select max(round(jsy,0)) from cp
亲测,有效,一句搞定
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐\ 经验分享
SQLite数据库的id字段,怎么设置成从1开始自增
使用自增长字段为主键有不少问题,比如维护或是在大型分布应用中主键冲突的解决等。在一些大型分布应用中主键一般选用guid,这可以有效的避免主键冲突,减少对主键维护的工程。当然,对于中小型的应用,自增长字段的好处更多一些,简单、快速。
Sqlite中,一个自增长字段定义为INTEGER PRIMARY KEY AUTOINCREMENT,那么在插入一个新数据时,只需要将这个字段的值指定为NULL,即可由引擎自动设定其值,引擎会设定为最大的rowid+1。当然,也可以设置为非NULL的数字来自己指定这个值,但这样就必须自己小心,不要引起冲突。当这个rowid的值大于所能表达的最大值 4775807 (3.0及以后版本的rowid最大值)后,rowid的新值会这个最大数之前随机找一个没被使用了的值。所以在rowid达到最大值前,rowid的值是严格单调增加的。
INTEGER PRIMARY KEY AUTOINCREMENT 自增长字段的算法与rowid稍微有些不同。
第一,在达到最大值后,rowid会找已被删除的字段对应的rowid作为新值,而自增长字段则会丢出一个SQLITE_FULL的错误。
第二,自增长字段在增加新值时,是找一个从没被使用过的rowid作为新值,而rowid则是找最大已存在的rowid+1。这里对应用的影响会比较大,尤其是一些对id值有依赖的元记录,只适合使用自增长字段而不能用rowid。
比如,我们设计一个元记录表:
create table test (
integer PRIMARY KEY autoincrement,
-- 设置主键
int default 0,
varchar (50),
[createdate]
datetime default (datetime('now', 'localtime'))
第三,使用自增长字段,引擎会自动产生一个sqlite_sequence表,用于记录每个表的自增长字段的已使用的最大值,用户可以看到,并可以用使用 Update、Delete和Insert操作,但不建议这么使用,这会让引擎混乱。如果使用rowid,也会有这么一个内部表,用户可以维护rowid 值,但看不到。
这么看来,如果直接使用rowid来代替自增加字段,根据两者的细微的差别,需要注意是否与自己的应用冲突,如果没有冲突,那么用rowid会更快一点。
SQLite中创建自增字段:
简单的回答:一个声明为 INTEGER PRIMARY KEY 的字段将自动增加。
从 SQLite 的 2.3.4 版本开始,如果你将一个表中的一个字段声明为 INTEGER PRIMARY KEY,那么无论你何时向该表的该字段插入一个 NULL 值,这个 NULL 值将自动被更换为比表中该字段所有行的最大值大 1 的整数;如果表为空,那么将被更换为 1。
一个新的API函数 sqlite3_last_insert_rowid() 返回最近的插入操作的整形键.
注意这个整型键始终比之前插入表中的最后一个键大1。新键相对于表中的已有键来说是唯一的,但它可能与之前从表中删除的键值重叠。要始终得到在整个表中唯一的键,在INTEGER PRIMARY KEY的声明之前加关键词AUTOINCREMENT.这样被选的键将总是比表中已存在的最大键大1。若可能的最大键已存在于表中,INSERT操作将失败并返回一个SQLITE_FULL错误码.
目前流行的数据库都提供了自动编号类型,SQLite也不例外。当数据库中包含自动编号的字段时,SQLite会自动建立一个名为 sqlite_sequence 的表。这个表包含两个字段:name 和 seq 。name字段记录了自动编号字段所在的表,seq字段记录了当前用到的序号(下一条记录的编号就是当前序号加1)。
在开发过程中,我们经常要把表重置。也就是说把表中的记录全部清空,并把自动编号归0。在SQLite中,只需要修改 sqlite_sequence 表就可以了:
复制代码 代码如下:
UPDATE sqlite_sequence SET seq = 0 WHERE name = 'TableName'
也可以直接把该记录删掉:
复制代码 代码如下:
DELETE FROM sqlite_sequence WHERE name = 'TableName'
要是想重置所有表,那直接把 sqlite_sequence 清空就可以了:
复制代码 代码如下:
DELETE FROM sqlite_sequence
若觉得本文不错,就分享一下吧!
作者的热门手记
请登录后,发表评论
评论加载中...
Copyright (C) 2018 imooc.com All Rights Reserved | 京ICP备 号-11请问sqlite中如何删除一条XX最小的数据_百度知道
请问sqlite中如何删除一条XX最小的数据
比如删除表table中,id最小的数据我用delete from table where id=min(id);报错,说min的用法错了,请问应该怎么写?
我有更好的答案
delete from table where id=(select min(id) from table)
采纳率:41%
来自团队:
为您推荐:
其他类似问题
您可能关注的内容
sqlite的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
id_html="zzzzz"require"luasql.sqlite3"env=assert(luasql.sqlite3())conn=assert(env:connect('info_db'))cursor,res=conn:execute([[select usbkeyid from user_list where userid=id_html]])//假如我把id_html换成‘zzzzz’就可以,但是使用id_html变量就不对,请问是因为在where userid=id_html这里表示id_html变量的方式不对吗?
如果不对,那么如何在select中进行参数查询呢?试了百度的很多方法都不行,求指教,万分感谢!!print(cursor)print(res)row,errorString=cursor:fetch({},"a")while row do
print(string.format("%s",row.v)) -- reusing the table of results row = cursor:fetch(row, "a")end
同步到新浪微博
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。

我要回帖

更多关于 最小可用版本 的文章

 

随机推荐