sqlite 句柄-sqlite 基础教程(3)

来源:互联网 发布:aspen plus软件 编辑:程序博客网 时间:2024/05/01 06:29

分类: IOS开发(所有IOS文章) ---sqlite(IOS) C/C++ 2457人阅读 评论(2) 收藏 举报
sqlitecallbackauthorizationfunction数据库transactions

声明
欢迎转载,但是请尊重作者劳动成果,转载请保留此框内声明,谢谢。
文章出处:http://blog.csdn.net/iukey




要操纵一个数据库你就得有一个这个数据库的句柄(又碰到这个难以理解的词了,不过确实还没得一个更好的词来替代它)。其实你跟本不需要去在乎这个词叫什么,你只要搞清楚他是一个什么玩意儿。就如同鞋子为什么叫鞋子,仔细想想确实也难以理解,不过 清楚他的功能就OK了,不是吗?

句柄在很多地方我们见到过,最常见的就是文件的句柄,我们要操纵一个文件,我们就要取得一个文件的句柄。句柄是个什么东东呢?其实很简单,句柄是一个东东的描述,他被定义为一个结构体,这个结构体可能会包含要描述的东东的具体信息,比如位置、大小、类型等等。我们有了这个描述信息我们就能去找到这个东东,然后操纵它。

一句话:句柄是物体的描述结构体。

我们来看看sqlite的句柄是怎么定义的(不要被吓到了,代码直接跳过就好):

[java] view plaincopyprint?
  1. struct sqlite3 {  
  2.   sqlite3_vfs *pVfs;            /* OS Interface */  
  3.   int nDb;                      /* Number of backends currently in use */  
  4.   Db *aDb;                      /* All backends */  
  5.   int flags;                    /* Miscellaneous flags. See below */  
  6.   unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */  
  7.   int errCode;                  /* Most recent error code (SQLITE_*) */  
  8.   int errMask;                  /* & result codes with this before returning */  
  9.   u8 autoCommit;                /* The auto-commit flag. */  
  10.   u8 temp_store;                /* 1: file 2: memory 0: default */  
  11.   u8 mallocFailed;              /* True if we have seen a malloc failure */  
  12.   u8 dfltLockMode;              /* Default locking-mode for attached dbs */  
  13.   signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */  
  14.   u8 suppressErr;               /* Do not issue error messages if true */  
  15.   u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */  
  16.   int nextPagesize;             /* Pagesize after VACUUM if >0 */  
  17.   int nTable;                   /* Number of tables in the database */  
  18.   CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */  
  19.   i64 lastRowid;                /* ROWID of most recent insert (see above) */  
  20.   u32 magic;                    /* Magic number for detect library misuse */  
  21.   int nChange;                  /* Value returned by sqlite3_changes() */  
  22.   int nTotalChange;             /* Value returned by sqlite3_total_changes() */  
  23.   sqlite3_mutex *mutex;         /* Connection mutex */  
  24.   int aLimit[SQLITE_N_LIMIT];   /* Limits */  
  25.   struct sqlite3InitInfo {      /* Information used during initialization */  
  26.     int iDb;                    /* When back is being initialized */  
  27.     int newTnum;                /* Rootpage of table being initialized */  
  28.     u8 busy;                    /* TRUE if currently initializing */  
  29.     u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */  
  30.   } init;  
  31.   int nExtension;               /* Number of loaded extensions */  
  32.   void **aExtension;            /* Array of shared library handles */  
  33.   struct Vdbe *pVdbe;           /* List of active virtual machines */  
  34.   int activeVdbeCnt;            /* Number of VDBEs currently executing */  
  35.   int writeVdbeCnt;             /* Number of active VDBEs that are writing */  
  36.   int vdbeExecCnt;              /* Number of nested calls to VdbeExec() */  
  37.   void (*xTrace)(void*,const char*);        /* Trace function */  
  38.   void *pTraceArg;                          /* Argument to the trace function */  
  39.   void (*xProfile)(void*,const char*,u64);  /* Profiling function */  
  40.   void *pProfileArg;                        /* Argument to profile function */  
  41.   void *pCommitArg;                 /* Argument to xCommitCallback() */     
  42.   int (*xCommitCallback)(void*);    /* Invoked at every commit. */  
  43.   void *pRollbackArg;               /* Argument to xRollbackCallback() */     
  44.   void (*xRollbackCallback)(void*); /* Invoked at every commit. */  
  45.   void *pUpdateArg;  
  46.   void (*xUpdateCallback)(void*,intconst char*,const char*,sqlite_int64);  
  47. #ifndef SQLITE_OMIT_WAL  
  48.   int (*xWalCallback)(void *, sqlite3 *, const char *, int);  
  49.   void *pWalArg;  
  50. #endif  
  51.   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);  
  52.   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);  
  53.   void *pCollNeededArg;  
  54.   sqlite3_value *pErr;          /* Most recent error message */  
  55.   char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */  
  56.   char *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */  
  57.   union {  
  58.     volatile int isInterrupted; /* True if sqlite3_interrupt has been called */  
  59.     double notUsed1;            /* Spacer */  
  60.   } u1;  
  61.   Lookaside lookaside;          /* Lookaside malloc configuration */  
  62. #ifndef SQLITE_OMIT_AUTHORIZATION  
  63.   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);  
  64.                                 /* Access authorization function */  
  65.   void *pAuthArg;               /* 1st argument to the access auth function */  
  66. #endif  
  67. #ifndef SQLITE_OMIT_PROGRESS_CALLBACK  
  68.   int (*xProgress)(void *);     /* The progress callback */  
  69.   void *pProgressArg;           /* Argument to the progress callback */  
  70.   int nProgressOps;             /* Number of opcodes for progress callback */  
  71. #endif  
  72. #ifndef SQLITE_OMIT_VIRTUALTABLE  
  73.   Hash aModule;                 /* populated by sqlite3_create_module() */  
  74.   VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */  
  75.   VTable **aVTrans;             /* Virtual tables with open transactions */  
  76.   int nVTrans;                  /* Allocated size of aVTrans */  
  77.   VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */  
  78. #endif  
  79.   FuncDefHash aFunc;            /* Hash table of connection functions */  
  80.   Hash aCollSeq;                /* All collating sequences */  
  81.   BusyHandler busyHandler;      /* Busy callback */  
  82.   int busyTimeout;              /* Busy handler timeout, in msec */  
  83.   Db aDbStatic[2];              /* Static space for the 2 default backends */  
  84.   Savepoint *pSavepoint;        /* List of active savepoints */  
  85.   int nSavepoint;               /* Number of non-transaction savepoints */  
  86.   int nStatement;               /* Number of nested statement-transactions  */  
  87.   u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */  
  88.   i64 nDeferredCons;            /* Net deferred constraints this transaction. */  
  89.   int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */  
  90.   
  91. #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY  
  92.   /* The following variables are all protected by the STATIC_MASTER  
  93.   ** mutex, not by sqlite3.mutex. They are used by code in notify.c.  
  94.   ** 
  95.   ** When X.pUnlockConnection==Y, that means that X is waiting for Y to 
  96.   ** unlock so that it can proceed. 
  97.   ** 
  98.   ** When X.pBlockingConnection==Y, that means that something that X tried 
  99.   ** tried to do recently failed with an SQLITE_LOCKED error due to locks 
  100.   ** held by Y. 
  101.   */  
  102.   sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */  
  103.   sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */  
  104.   void *pUnlockArg;                     /* Argument to xUnlockNotify */  
  105.   void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */  
  106.   sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */  
  107. #endif  
  108. };  

[java] view plaincopyprint?
  1. typedef struct sqlite3 sqlite3;//  

是不是被吓到了,没关系,这段代码本来就是我贴出来吓唬你的,我也没认真研究过这个代码,也不想去研究,除非哪天有人出高价请我去研究,我现在只知道怎么用就好了。

这个 sqlite3 结构体就是被用来描述我们磁盘里的数据库文件的,有了这个描述符我们就可以对这个数据库进行各种操作了,操作的具体内情我们不必要了解,我们只需要知道怎么去调用API就好了,当然有时候还是要了解一点点内情的,这个以后碰到再讲。

我用这么长的话加一大段吓人的代码只有一个目的:不要对句柄有恐惧感。

好了,开始我们的正题,sqlite 里面你要操纵数据库我们先得创建一个句柄,然后后面所有对数据库得操作都会用到这个句柄。

[java] view plaincopyprint?
  1. sqlite3* pdb;  
就 这么简单,这样一个ssqlite的句柄我们就创建完成了,我们以后针对数据库的操作都离不开它了。你可能还有疑问,我也知道你的 疑问是什么,请看下回分解。