使用了下Sqlite3

前端之家收集整理的这篇文章主要介绍了使用了下Sqlite3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用了sqlite3,有些感受:
基本的方法就是sqlite3_column_*、sqlite3_prepare_*、sqlite3_bind_*、sqlite3_step等sqlite3_exec都是上述的方法组合出来的,观察legecy.c的源代码可知。
感觉sqlite3的语法结构怪怪的,exec方法居然使用了循环来实现,不知道是作者思考方法和我不同,还是自己C语言用的不熟练。
老是存在指针的类型转换问题,看来指针学的不好,真对不同的情况在callback回调函数和基本的s qlite3_column_*中做出选择。
code in legecy.c version 3.3.10
/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice,here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely,never taking more than you give.
**
*************************************************************************
** Main file for the sqlite library. The routines in this file
** implement the programmer interface to the library. Routines in
** other files are for internal use by sqlite and should not be
** accessed by users of the library.
**
** $Id: legacy.c,v 1.16 2006/09/15 07:28:50 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>
/*
** Execute sql code. Return one of the sqlITE_ success/failure
** codes. Also write an error message into memory obtained from
** malloc() and make *pzErrMsg point to that message.
**
** If the sql is a query,then for each row in the query result
** the xCallback() function is called. pArg becomes the first
** argument to xCallback(). If xCallback=NULL then no callback
** is invoked,even for queries.
*/
int sqlite3_exec(
sqlite3 *db,/* The database on which the sql executes */
const char *zsql,/* The sql to be executed */
sqlite3_callback xCallback,/* Invoke this callback routine */
void *pArg,/* First argument to xCallback() */
char **pzErrMsg /* Write error messages here */
){
int rc = sqlITE_OK;
const char *zLeftover;
sqlite3_stmt *pStmt = 0;
char **azCols = 0;
int nRetry = 0;
int nChange = 0;
int nCallback;
if( zsql==0 ) return sqlITE_OK;
while( (rc==sqlITE_OK || (rc==sqlITE_SCHEMA && (++nRetry)<2)) && zsql[0] ){
int nCol;
char **azVals = 0;
pStmt = 0;
rc = sqlite3_prepare(db,zsql,-1,&pStmt,&zLeftover);
assert( rc==sqlITE_OK || pStmt==0 );
if( rc!=sqlITE_OK ){
continue;
}
if( !pStmt ){
/* this happens for a comment or white-space */
zsql = zLeftover;
continue;
}
db->nChange += nChange;
nCallback = 0;
nCol = sqlite3_column_count(pStmt);
azCols = sqliteMalloc(2*nCol*sizeof(const char *) + 1);
if( azCols==0 ){
goto exec_out;
}
while( 1 ){
int i;
rc = sqlite3_step(pStmt);
/* Invoke the callback function if required */
if( xCallback && (sqlITE_ROW==rc ||
(sqlITE_DONE==rc && !nCallback && db->flags&sqlITE_NullCallback)) ){
if( 0==nCallback ){
for(i=0; i<nCol; i++){
azCols[i] = (char *)sqlite3_column_name(pStmt,i);
}
nCallback++;
}
if( rc==sqlITE_ROW ){
azVals = &azCols[nCol];
for(i=0; i<nCol; i++){
azVals[i] = (char *)sqlite3_column_text(pStmt,i);
}
}
if( xCallback(pArg,nCol,azVals,azCols) ){
rc = sqlITE_ABORT;
goto exec_out;
}
}
if( rc!=sqlITE_ROW ){
rc = sqlite3_finalize(pStmt);
pStmt = 0;
if( db->pVdbe==0 ){
nChange = db->nChange;
}
if( rc!=sqlITE_SCHEMA ){
nRetry = 0;
zsql = zLeftover;
while( isspace((unsigned char)zsql[0]) ) zsql++;
}
break;
}
}
sqliteFree(azCols);
azCols = 0;
}
exec_out:
if( pStmt ) sqlite3_finalize(pStmt);
if( azCols ) sqliteFree(azCols);
rc = sqlite3ApiExit(0,rc);
if( rc!=sqlITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
*pzErrMsg = sqlite3_malloc(1+strlen(sqlite3_errmsg(db)));
if( *pzErrMsg ){
strcpy(*pzErrMsg,sqlite3_errmsg(db));
}
}else if( pzErrMsg ){
*pzErrMsg = 0;
}
assert( (rc&db->errMask)==rc ); return rc; }
原文链接:https://www.f2er.com/sqlite/203248.html

猜你在找的Sqlite相关文章