要操纵一个数据库你就得有一个这个数据库的句柄(又碰到这个难以理解的词了,不过确实还没得一个更好的词来替代它)。其实你跟本不需要去在乎这个词 叫什么,你只要搞清楚他是一个什么玩意儿。就如同鞋子为什么叫鞋子,仔细想想确实也难以理解,不过 清楚他的功能就OK了,不是吗?
句柄在很多地方我们见到过,最常见的就是文件的句柄,我们要操纵一个文件,我们就要取得一个文件的句柄。句柄是个什么东东呢?其实很简单,句柄是一 个东东的描述,他被定义为一个结构体,这个结构体可能会包含要描述的东东的具体信息,比如位置、大小、类型等等。我们有了这个描述信息我们就能去找到这个 东东,然后操纵它。
一句话:句柄是物体的描述结构体。
我们来看看sqlite的句柄是怎么定义的(不要被吓到了,代码直接跳过就好):
源码
打印
?
- structsqlite3{
- sqlite3_vfs*pVfs;/*OSInterface*/
- intnDb;/*Numberofbackendscurrentlyinuse*/
- Db*aDb;/*Allbackends*/
- intflags;/*Miscellaneousflags.Seebelow*/
- unsignedintopenFlags;/*Flagspassedtosqlite3_vfs.xOpen()*/
- interrCode;/*Mostrecenterrorcode(SQLITE_*)*/
- interrMask;/*&resultcodeswiththisbeforereturning*/
- u8autoCommit;/*Theauto-commitflag.*/
- u8temp_store;/*1:file2:memory0:default*/
- u8mallocFailed;/*Trueifwehaveseenamallocfailure*/
- u8dfltLockMode;/*Defaultlocking-modeforattacheddbs*/
- signedcharnextAutovac;/*AutovacsettingafterVACUUMif>=0*/
- u8suppressErr;/*Donotissueerrormessagesiftrue*/
- u8vtabOnConflict;/*Valuetoreturnfors3_vtab_on_conflict()*/
- intnextPagesize;/*PagesizeafterVACUUMif>0*/
- intnTable;/*Numberoftablesinthedatabase*/
- CollSeq*pDfltColl;/*Thedefaultcollatingsequence(BINARY)*/
- i64lastRowid;/*ROWIDofmostrecentinsert(seeabove)*/
- u32magic;/*Magicnumberfordetectlibrarymisuse*/
- intnChange;/*Valuereturnedbysqlite3_changes()*/
- intnTotalChange;/*Valuereturnedbysqlite3_total_changes()*/
- sqlite3_mutex*mutex;/*Connectionmutex*/
- intaLimit[SQLITE_N_LIMIT];/*Limits*/
- structsqlite3InitInfo{/*Informationusedduringinitialization*/
- intiDb;/*Whenbackisbeinginitialized*/
- intnewTnum;/*Rootpageoftablebeinginitialized*/
- u8busy;/*TRUEifcurrentlyinitializing*/
- u8orphanTrigger;/*LaststatementisorphanedTEMPtrigger*/
- }init;
- intnExtension;/*Numberofloadedextensions*/
- void**aExtension;/*Arrayofsharedlibraryhandles*/
- structVdbe*pVdbe;/*Listofactivevirtualmachines*/
- intactiveVdbeCnt;/*NumberofVDBEscurrentlyexecuting*/
- intwriteVdbeCnt;/*NumberofactiveVDBEsthatarewriting*/
- intvdbeExecCnt;/*NumberofnestedcallstoVdbeExec()*/
- void(*xTrace)(void*,constchar*);/*Tracefunction*/
- void*pTraceArg;/*Argumenttothetracefunction*/
- void(*xProfile)(void*,constchar*,u64);/*Profilingfunction*/
- void*pProfileArg;/*Argumenttoprofilefunction*/
- void*pCommitArg;/*ArgumenttoxCommitCallback()*/
- int(*xCommitCallback)(void*);/*Invokedateverycommit.*/
- void*pRollbackArg;/*ArgumenttoxRollbackCallback()*/
- void(*xRollbackCallback)(void*);/*Invokedateverycommit.*/
- void*pUpdateArg;
- void(*xUpdateCallback)(void*,int,sqlite_int64);
- #ifndefSQLITE_OMIT_WAL
- int(*xWalCallback)(void*,sqlite3*,int);
- void*pWalArg;
- #endif
- void(*xCollNeeded)(void*,inteTextRep,constchar*);
- void(*xCollNeeded16)(void*,constvoid*);
- void*pCollNeededArg;
- sqlite3_value*pErr;/*Mostrecenterrormessage*/
- char*zErrMsg;/*Mostrecenterrormessage(UTF-8encoded)*/
- char*zErrMsg16;/*Mostrecenterrormessage(UTF-16encoded)*/
- union{
- volatileintisInterrupted;/*Trueifsqlite3_interrupthasbeencalled*/
- doublenotUsed1;/*Spacer*/
- }u1;
- Lookasidelookaside;/*Lookasidemallocconfiguration*/
- #ifndefSQLITE_OMIT_AUTHORIZATION
- int(*xAuth)(void*,constchar*);
- /*Accessauthorizationfunction*/
- void*pAuthArg;/*1stargumenttotheaccessauthfunction*/
- #endif
- #ifndefSQLITE_OMIT_PROGRESS_CALLBACK
- int(*xProgress)(void*);/*Theprogresscallback*/
- void*pProgressArg;/*Argumenttotheprogresscallback*/
- intnProgressOps;/*Numberofopcodesforprogresscallback*/
- #endif
- #ifndefSQLITE_OMIT_VIRTUALTABLE
- HashaModule;/*populatedbysqlite3_create_module()*/
- VtabCtx*pVtabCtx;/*Contextforactivevtabconnect/create*/
- VTable**aVTrans;/*Virtualtableswithopentransactions*/
- intnVTrans;/*AllocatedsizeofaVTrans*/
- VTable*pDisconnect;/*Disconnecttheseinnextsqlite3_prepare()*/
- #endif
- FuncDefHashaFunc;/*Hashtableofconnectionfunctions*/
- HashaCollSeq;/*Allcollatingsequences*/
- BusyHandlerbusyHandler;/*Busycallback*/
- intbusyTimeout;/*Busyhandlertimeout,inmsec*/
- DbaDbStatic[2];/*Staticspaceforthe2defaultbackends*/
- Savepoint*pSavepoint;/*Listofactivesavepoints*/
- intnSavepoint;/*Numberofnon-transactionsavepoints*/
- intnStatement;/*Numberofnestedstatement-transactions*/
- u8isTransactionSavepoint;/*TrueiftheoutermostsavepointisaTS*/
- i64nDeferredCons;/*Netdeferredconstraintsthistransaction.*/
- int*pnBytesFreed;/*IfnotNULL,incrementthisinDbFree()*/
-
- #ifdefSQLITE_ENABLE_UNLOCK_NOTIFY
- /*ThefollowingvariablesareallprotectedbytheSTATIC_MASTER
- **mutex,notbysqlite3.mutex.Theyareusedbycodeinnotify.c.
- **
- **WhenX.pUnlockConnection==Y,thatmeansthatXiswaitingforYto
- **unlocksothatitcanproceed.
- **
- **WhenX.pBlockingConnection==Y,thatmeansthatsomethingthatXtried
- **triedtodorecentlyfailedwithanSQLITE_LOCKEDerrorduetolocks
- **heldbyY.
- */
- sqlite3*pBlockingConnection;/*ConnectionthatcausedSQLITE_LOCKED*/
- sqlite3*pUnlockConnection;/*Connectiontowatchforunlock*/
- void*pUnlockArg;/*ArgumenttoxUnlockNotify*/
- void(*xUnlockNotify)(void**,int);/*Unlocknotifycallback*/
- sqlite3*pNextBlocked;/*Nextinlistofallblockedconnections*/
- #endif
- };
?
- typedefstructsqlite3sqlite3;//
是不是被吓到了,没关系,这段代码本来就是我贴出来吓唬你的,我也没认真研究过这个代码,也不想去研究,除非哪天有人出高价请我去研究,我现在只知道怎么用就好了。
这个 sqlite3 结构体就是被用来描述我们磁盘里的数据库文件的,有了这个描述符我们就可以对这个数据库进行各种操作了,操作的具体内情我们不必要了解,我们只需要知道怎么去调用API就好了,当然有时候还是要了解一点点内情的,这个以后碰到再讲。
我用这么长的话加一大段吓人的代码只有一个目的:不要对句柄有恐惧感。
好了,开始我们的正题,sqlite 里面你要操纵数据库我们先得创建一个句柄,然后后面所有对数据库得操作都会用到这个句柄。
?
- sqlite3*pdb;
就 这么简单,这样一个ssqlite的句柄我们就创建完成了,我们以后针对数据库的操作都离不开它了。你可能还有疑问,我也知道你的 疑问是什么,请看下回分解。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|