嵌入式 Sqlite3学习小结

嵌入式 Sqlite3学习小结,第1张

概述在终端上,程序名称是sqlite3 sqlite3 -help来看此程序用法 常用: sqlite3 FileName,来进入某一个特定数据库文件 sqlite3 -version,查看版本 好,实践录入: mac-Pro:~ user$sqlite3 testdb sqlite> 在这里可以使用标准的ddl和dml语句,比如: create table t1 ......; insert int 在终端上,程序名称是sqlite3

sqlite3 -help来看此程序用法

常用:
sqlite3 filename,来进入某一个特定数据库文件
sqlite3 -version,查看版本

好,实践录入:
mac-Pro:~ user$sqlite3 testdb

sqlite>

在这里可以使用标准的ddl和dml语句,比如:
create table t1 ......;
insert into t1 (...) values (...);
Select * from t1;

这里命令都以;作为结束符号

除了标准的SQL语句外,还有一些用法,用于管理的命令,这类命令以.开头,可以输入.help来阅读

这里列举简单的命令:
.databases
.tables
.exit
.quit

至于其他命令用.help来阅读

说到这里,在终端上使用sqlite3基本上实践了下

这里回到开发上来

以C\C++接口来说,官方资料http://www.sqlite.org/cAPI3.HTML

1、
sqlite3的类型,只有以下几类:
#define sqlITE_INTEGER 1
#define sqlITE_float 2
#define sqlITE_TEXT 3
#define sqlITE_BLOB 4
#define sqlITE_NulL 5


涉及函数:
int sqlite3_column_type(sqlite3_stmt*,int iCol);返回列类型

2、
打开sqlite数据库,关闭sqlite数据库
int sqlite3_open(const char*,sqlite3**);
int sqlite3_open16(const voID*,sqlite3**);
int sqlite3_close(sqlite3*);


默认编码utf-8,第二个函数使用utf-16
返回整型错误码
#define sqlITE_OK 0 /* Successful result */
#define sqlITE_ERROR 1 /* sql error or missing database */
#define sqlITE_INTERNAL 2 /* An internal logic error in sqlite */
#define sqlITE_PERM 3 /* Access permission denIEd */
#define sqlITE_ABORT 4 /* Callback routine requested an abort */
#define sqlITE_BUSY 5 /* The database file is locked */
#define sqlITE_LOCKED 6 /* A table in the database is locked */
#define sqlITE_NOMEM 7 /* A malloc() Failed */
#define sqlITE_Readonly 8 /* Attempt to write a Readonly database */
#define sqlITE_INTERRUPT 9 /* Operation terminated by sqlite_interrupt() */
#define sqlITE_IOERR 10 /* Some kind of disk I/O error occurred */
#define sqlITE_CORRUPT 11 /* The database disk image is malformed */
#define sqlITE_NOTFOUND 12 /* (Internal Only) table or record not found */
#define sqlITE_FulL 13 /* Insertion Failed because database is full */
#define sqlITE_CANtopEN 14 /* Unable to open the database file */
#define sqlITE_PROTOCol 15 /* Database lock protocol error */
#define sqlITE_EMPTY 16 /* (Internal Only) Database table is empty */
#define sqlITE_SCHEMA 17 /* The database schema changed */
#define sqlITE_TOOBIG 18 /* Too much data for one row of a table */
#define sqlITE_CONSTRAINT 19 /* Abort due to contraint violation */
#define sqlITE_MISMATCH 20 /* Data type mismatch */
#define sqlITE_MISUSE 21 /* library used incorrectly */
#define sqlITE_NolFS 22 /* Uses OS features not supported on host */
#define sqlITE_AUTH 23 /* Authorization denIEd */
#define sqlITE_ROW 100 /* sqlite_step() has another row ready */
#define sqlITE_DONE 101 /* sqlite_step() has finished executing */


实践代码:
sqlite3* database;
Nsstring* path;//need you set

if(sqlite3_open([path UTF8String],&database) == sqlITE_OK)
{
NSLog(@"DB created and opened");
}

sqlite3_close(database);

3、
数据 *** 纵语句-insert,update,delete

In sqlite version 3,the sqlite3_exec routine is just a wrapper around calls to the prepared statement interface.
在sqlite3下,程序sqlite3_exec【这个工作在sqlite2下】被分装调用在预处理语句接口

typedef struct sqlite3_stmt sqlite3_stmt;
int sqlite3_prepare(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
int sqlite3_prepare16(sqlite3*,const voID*,const voID**);
int sqlite3_finalize(sqlite3_stmt*);
int sqlite3_reset(sqlite3_stmt*);


预处理语句后,应该使用这个函数来执行:
int sqlite3_step(sqlite3_stmt*);
The sqlite3_step() routine return sqlITE_ROW if it is returning a single row of the result set,or sqlITE_DONE if execution has completed,either normally or due to an error.

如果有完整SQL语句,可以用以下来直接调用
-(BOol) Execsql:(char *)sql
{
BOol isOK = NO;
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(self->_database,sql,-1,&statement,nil) != sqlITE_OK)
{
isOK = NO;
}
else
{
int success = sqlite3_step(statement);
sqlite3_finalize(statement);
if (success != sqlITE_DONE)
{
isOK = NO;
}
else
{
isOK = YES;
}
}

return isOK;
}

还有一种,类似参数化语句的执行方式,需要绑定参数,绑定函数有,
int sqlite3_bind_blob(sqlite3_stmt*,int n,voID(*)(voID*));
int sqlite3_bind_double(sqlite3_stmt*,double);
int sqlite3_bind_int(sqlite3_stmt*,int);
int sqlite3_bind_int64(sqlite3_stmt*,long long int);
int sqlite3_bind_null(sqlite3_stmt*,int);
int sqlite3_bind_text(sqlite3_stmt*,voID(*)(voID*));
int sqlite3_bind_text16(sqlite3_stmt*,voID(*)(voID*));
int sqlite3_bind_value(sqlite3_stmt*,const sqlite3_value*);


The second argument is the index of the sql parameter to be set. The leftmost sql parameter has an index of 1.
第二个参数是sql参数的索引值,以1开始

看下此代码:
-(voID) SaveMBKey:(Nsstring *)key
{
BOol isOK = NO;

sqlite3_stmt *statement;

static char* sql ="Insert Into MBKey (key) values (?);";

int success = sqlite3_prepare_v2(self->_database,NulL);
if (success != sqlITE_OK)
{
isOK = NO;
}
else
{
sqlite3_bind_text(statement,1,[key UTF8String],sqlITE_TRANSIENT);

success = sqlite3_step(statement);
sqlite3_finalize(statement);
}

if (success == sqlITE_ERROR)
{
isOK = NO;
}
else
{
isOK = YES;
}

return;
}

4、
查询和填充,此处理和上述类似,唯一的不同需要列值绑定

当sqlite3_step返回sqlITE_ROW,需要以下函数来接收数据

const voID *sqlite3_column_blob(sqlite3_stmt*,int iCol); int sqlite3_column_bytes(sqlite3_stmt*,int iCol); int sqlite3_column_bytes16(sqlite3_stmt*,int iCol); double sqlite3_column_double(sqlite3_stmt*,int iCol); int sqlite3_column_int(sqlite3_stmt*,int iCol); sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*,int iCol); const unsigned char *sqlite3_column_text(sqlite3_stmt*,int iCol); const voID *sqlite3_column_text16(sqlite3_stmt*,int iCol); int sqlite3_column_type(sqlite3_stmt*,int iCol); sqlite3_value *sqlite3_column_value(sqlite3_stmt*,int iCol); These routines form the "result set" interface. the second argument is the index of the column for which information should be returned. The leftmost column of the result set has the index 0. 注意:iCol参数起始值是0 好,看下下面的实践代码: -(voID) GetList:(NSMutableArray*)KeysList { BOol isOK = NO; sqlite3_stmt *statement; static char* sql ="Select KeyID,Key From MBKey Order by KeyID;"; int success = sqlite3_prepare_v2(self->_database,NulL); if (success != sqlITE_OK) { isOK = NO; } else { //查询结果集中一条一条的遍历所有的记录,这里的数字对应的是列值。 while (sqlite3_step(statement) == sqlITE_ROW) { int kID = sqlite3_column_int(statement,0); char* key = (char*)sqlite3_column_text(statement,1); KeyInfo* keyInfo = [[KeyInfo alloc] init]; if(key) [keyInfo.Key setString:[Nsstring stringWithUTF8String:key]]; keyInfo.KeyID = kID; [KeysList addobject:keyInfo]; [keyInfo release]; } sqlite3_finalize(statement); } if (success == sqlITE_ERROR) { isOK = NO; } else { isOK = YES; } return; } 说明下:在当前实践环境下,可以支持inner join,支持left join,不支持right out join 和full join select * from a inner join b on a.ID = b.ID; select * from a join b on a.ID = b.ID; select * from a left join b on a.ID = b.ID; select * from a right outer join b on a.ID = b.ID;//报错,不支持 好,使用编程的方式能对sqlite数据库进行基础 *** 作,基本上能开发小型的db程序了 参考sqlite不支持sql语法文档:http://wenku.baIDu.com/vIEw/823c3e92daef5ef7ba0d3cd5.HTML /****************/ 关于sqlite3文件格式描述地址: http://www.sqlite.org/fileformat2.HTML /****************/ 5、sql字符处理 A、subStr(X,Y,Z),X字段名,Y以1作为起始字符索引值,缩写函数是 subStr(X,Y) B、like语句,支持_,%通配符 C、字符拼接使用“||”,eg:select 'a'||'b' from c;输出ab [ABC,2012-07-02补充] 6、日期数据处理 存储日期数据建议采用'%Y-%m-%d'或'%Y-%m-%d %H:%M:%s',其中月和日均为2位,不足以0补高位 参看官网支持格式: YYYY-MM-DD YYYY-MM-DD HH:MM YYYY-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS.SSS YYYY-MM-DDTHH:MM YYYY-MM-DDTHH:MM:SS YYYY-MM-DDTHH:MM:SS.SSS HH:MM HH:MM:SS HH:MM:SS.SSS 常用sqlite日期格式范例: select date(); 2012-07-04 select datetime(); 2012-07-04 02:47:23 select strftime('%Y','Now' ) 2012 select strftime('%m','Now' ) 07 select strftime('%d','Now' ) 04 select strftime('%s','Now' ) 1341371148//返回当前时间离1970-1-1的秒数 SELECT date('Now','start of month','+1 month','-1 day'); 2012-07-31//当前月份最后一天 SELECT julianday(date()) - julianday('2012-07-01'); 3//当前2012-07-04离2012-7-1的天数 good luck
总结

以上是内存溢出为你收集整理的嵌入式 Sqlite3学习小结全部内容,希望文章能够帮你解决嵌入式 Sqlite3学习小结所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1171751.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存