sqlite

How to tell if sqlite database file is valid or not

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-20 06:36:28
问题 In the code below, pathToNonDatabase is the path to a simple text file, not a real sqlite database. I was hoping for sqlite3_open to detect that, but it doesn't ( db is not NULL , and result is SQLITE_OK ). So, how to detect that a file is not a valid sqlite database? sqlite3 *db = NULL; int result = sqlite3_open(pathToNonDatabase, &db); if((NULL==db) || (result!=SQLITE_OK)) { // invalid database } 回答1: sqlite opens databases lazily. Just do something immediately after opening that requires

SQLite错误码

穿精又带淫゛_ 提交于 2020-02-20 04:46:40
在SQLite中,执行SQL语句的sqlite3_exec()和sqlite3_prepare()两个核心方法的返回值都是一个整型数据,因此,当程序执行出现错误时,我们可以根据执行返回的整型数据来判断错误发生的原因。以下就是SQLite的错误码: 1 #define SQLITE_OK 0 /* 成功 | Successful result */ 2 /* 错误码开始 */ 3 #define SQLITE_ERROR 1 /* SQL错误 或 丢失数据库 | SQL error or missing database */ 4 #define SQLITE_INTERNAL 2 /* SQLite 内部逻辑错误 | Internal logic error in SQLite */ 5 #define SQLITE_PERM 3 /* 拒绝访问 | Access permission denied */ 6 #define SQLITE_ABORT 4 /* 回调函数请求取消操作 | Callback routine requested an abort */ 7 #define SQLITE_BUSY 5 /* 数据库文件被锁定 | The database file is locked */ 8 #define SQLITE_LOCKED 6 /* 数据库中的一个表被锁定 |

sqlite 性能优化

青春壹個敷衍的年華 提交于 2020-02-18 18:08:07
主要通过pragma指令来实现。 比如: 空间释放、磁盘同步、Cache大小等。 不要打开。前文提高了,Vacuum的效率非常低! PRAGMA auto_vacuum; PRAGMA auto_vacuum = 0 | 1; 查询或设置数据库的auto-vacuum标记。 正常情况下,当提交一个从数据库中删除数据的事务时,数据库文件不改变大小。未使用的文件页被标记并在以后的添加操作中 再次使用。这种情况下使用VACUUM命令释放删除得到的空间。 当开启auto-vacuum,当提交一个从数据库中删除数据的事务时,数据库文件自动收缩, (VACUUM命令在auto-vacuum开启的数据库中不起作用)。数据库会在内部存储一些信息以便支持这一功能,这使得 数据库文件比不开启该选项时稍微大一些。 只有在数据库中未建任何表时才能改变auto-vacuum标记。试图在已有表的情况下修改不会导致报错。 建议改为8000 PRAGMA cache_size; PRAGMA cache_size = Number-of-pages; 查询或修改SQLite一次存储在内存中的数据库文件页数。每页使用约1.5K内存,缺省的缓存大小是2000. 若需要使用改变大量多行的UPDATE或DELETE命令,并且不介意SQLite使用更多的内存的话,可以增大缓存以提高性能。 当使用cache_size

sqlite 3基本使用方法

ぃ、小莉子 提交于 2020-02-18 08:07:46
1、sqlite数据库数据类型   Integer      整型   varchar(10)    字符数组   float       浮点型   double      双精度浮点型   char(10)      字符型   text        文本型 2、sql语法 2.1 创建表语句   create table 表名(字段名称 数据类型 约束,字段名称 数据类型 约束......)   create table person(id Integer primary key,name varchar(10),age Integer not null) 2.2 删除表的语句   drop table 表名   drop table person 2.3 插入数据   insert into 表名(字段,字段) values(值1,值2)   insert into person(id,age) values(1,20)   insert into person values(2,"qwe",30)-->如果没有指定字段名称,则values中的值必须从表结构中第一个字段开始依次赋值 2.4 修改数据   update 表名 set 字段=新值 where 修改的条件   update person set name=“ls” where id=1   note

SQLite中的自增关键字:AUTO_INCREMENT、INTEGER PRIMARY KEY与AUTOINCREMENT

假如想象 提交于 2020-02-18 07:10:46
1、SQLite不支持关键字AUTO_INCREMENT 1)AUTO_INCREMENT不生效的问题 SQL语句: CREATE TABLE todo ( id INTEGER AUTO_INCREMENT, title TEXT, PRIMARY KEY (id) ); 问题描述:按照上述SQL语句创建表todo,用INSERT INTO todo (title) VALUES ('xxx')插入记录,但查询该记录后得到的id为NULL(即Python中的None) 实验脚本: #!/usr/bin/python # -*- encoding: utf-8 -*- import sqlite3 con = sqlite3.connect(":memory:") # 创建表 con.execute(""" CREATE TABLE todo ( id INTEGER AUTO_INCREMENT, title TEXT, PRIMARY KEY (id) );""") # 插入记录 con.execute("INSERT INTO todo (title) VALUES ('shopping');") # 查询记录 for row in con.execute("SELECT * FROM todo"): print row 运行结果: $ python auto

SwiftUI Bug - List changes lock UI - (OLD TITLE: SwiftUI CoreData fetch is very slow)

ぐ巨炮叔叔 提交于 2020-02-18 05:52:11
问题 Update #4 I've Reordered this post to read a little more easily. What you will read below will detail a bug I've experienced using SwiftUI. I recently requested code level support from apple who confirmed same and asked that I reach out to feedback for resolution (which was also done, no answer yet). The bug is this : After displaying a List or ForEach in a SwiftUI View, if you alter that view by changing the number of items listed, the UI locks up while it attempts to calculate the number of

SwiftUI Bug - List changes lock UI - (OLD TITLE: SwiftUI CoreData fetch is very slow)

爷,独闯天下 提交于 2020-02-18 05:51:36
问题 Update #4 I've Reordered this post to read a little more easily. What you will read below will detail a bug I've experienced using SwiftUI. I recently requested code level support from apple who confirmed same and asked that I reach out to feedback for resolution (which was also done, no answer yet). The bug is this : After displaying a List or ForEach in a SwiftUI View, if you alter that view by changing the number of items listed, the UI locks up while it attempts to calculate the number of

Limit number of Rows inserted into a Table

核能气质少年 提交于 2020-02-16 05:13:46
问题 How can we restrict a table to have fixed number of rows? for example if i give Limit as 20, then 20 rows can be inserted into it and after that table should indicate the limit has been exceeded. EXAMPLE: IF we create a trigger CREATE TRIGGER log AFTER INSERT ON TEST_TABLE BEGIN INSERT INTO TEST_TABLE VALUES(....); SELECT COUNT(COL) FROM TEST_TABLE; END; This is not giving me the count as soon as i enter a row into the table. 回答1: Create a Trigger before Insert on That table . In Trigger you

SQlite query to update column and replace a value

前提是你 提交于 2020-02-16 03:53:12
问题 I want to update a column that contains a string like 12,43,433 I want to only replace 43 with another number say 54 so that the column value becomes 12,54,433. How can I do that?? 回答1: Storing lists as strings is a very bad idea. SQL has a great data structure for storing lists. It is called a table, not a string. The proper way to store lists is to use a junction table. Sometimes we are stuck with other people's really bad design decisions. If so, you can do: update t set col = trim(replace

SQlite query to update column and replace a value

牧云@^-^@ 提交于 2020-02-16 03:52:32
问题 I want to update a column that contains a string like 12,43,433 I want to only replace 43 with another number say 54 so that the column value becomes 12,54,433. How can I do that?? 回答1: Storing lists as strings is a very bad idea. SQL has a great data structure for storing lists. It is called a table, not a string. The proper way to store lists is to use a junction table. Sometimes we are stuck with other people's really bad design decisions. If so, you can do: update t set col = trim(replace