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

前端之家收集整理的这篇文章主要介绍了sqlite 句柄-sqlite 基础教程(3)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

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

  1. structsqlite3{
  2. sqlite3_vfs*pVfs;/*OSInterface*/
  3. intnDb;/*Numberofbackendscurrentlyinuse*/
  4. Db*aDb;/*Allbackends*/
  5. intflags;/*Miscellaneousflags.Seebelow*/
  6. unsignedintopenFlags;/*Flagspassedtosqlite3_vfs.xOpen()*/
  7. interrCode;/*Mostrecenterrorcode(sqlITE_*)*/
  8. interrMask;/*&resultcodeswiththisbeforereturning*/
  9. u8autoCommit;/*Theauto-commitflag.*/
  10. u8temp_store;/*1:file2:memory0:default*/
  11. u8mallocFailed;/*Trueifwehaveseenamallocfailure*/
  12. u8dfltLockMode;/*Defaultlocking-modeforattacheddbs*/
  13. signedcharnextAutovac;/*AutovacsettingafterVACUUMif>=0*/
  14. u8suppressErr;/*Donotissueerrormessagesiftrue*/
  15. u8vtabOnConflict;/*Valuetoreturnfors3_vtab_on_conflict()*/
  16. intnextPagesize;/*PagesizeafterVACUUMif>0*/
  17. intnTable;/*Numberoftablesinthedatabase*/
  18. CollSeq*pDfltColl;/*Thedefaultcollatingsequence(BINARY)*/
  19. i64lastRowid;/*ROWIDofmostrecentinsert(seeabove)*/
  20. u32magic;/*Magicnumberfordetectlibrarymisuse*/
  21. intnChange;/*Valuereturnedbysqlite3_changes()*/
  22. intnTotalChange;/*Valuereturnedbysqlite3_total_changes()*/
  23. sqlite3_mutex*mutex;/*Connectionmutex*/
  24. intaLimit[sqlITE_N_LIMIT];/*Limits*/
  25. structsqlite3InitInfo{/*Informationusedduringinitialization*/
  26. intiDb;/*Whenbackisbeinginitialized*/
  27. intnewTnum;/*Rootpageoftablebeinginitialized*/
  28. u8busy;/*TRUEifcurrentlyinitializing*/
  29. u8orphanTrigger;/*LaststatementisorphanedTEMPtrigger*/
  30. }init;
  31. intnExtension;/*Numberofloadedextensions*/
  32. void**aExtension;/*Arrayofsharedlibraryhandles*/
  33. structVdbe*pVdbe;/*Listofactivevirtualmachines*/
  34. intactiveVdbeCnt;/*NumberofVDBEscurrentlyexecuting*/
  35. intwriteVdbeCnt;/*NumberofactiveVDBEsthatarewriting*/
  36. intvdbeExecCnt;/*NumberofnestedcallstoVdbeExec()*/
  37. void(*xTrace)(void*,constchar*);/*Tracefunction*/
  38. void*pTraceArg;/*Argumenttothetracefunction*/
  39. void(*xProfile)(void*,constchar*,u64);/*Profilingfunction*/
  40. void*pProfileArg;/*Argumenttoprofilefunction*/
  41. void*pCommitArg;/*ArgumenttoxCommitCallback()*/
  42. int(*xCommitCallback)(void*);/*Invokedateverycommit.*/
  43. void*pRollbackArg;/*ArgumenttoxRollbackCallback()*/
  44. void(*xRollbackCallback)(void*); void*pUpdateArg;
  45. void(*xUpdateCallback)(void*,int,sqlite_int64);
  46. #ifndefsqlITE_OMIT_WAL
  47. int(*xWalCallback)(void*,sqlite3*,int);
  48. void*pWalArg;
  49. #endif
  50. void(*xCollNeeded)(void*,inteTextRep,constchar*);
  51. void(*xCollNeeded16)(void*,constvoid*);
  52. void*pCollNeededArg;
  53. sqlite3_value*pErr;/*Mostrecenterrormessage*/
  54. char*zErrMsg;/*Mostrecenterrormessage(UTF-8encoded)*/
  55. char*zErrMsg16;/*Mostrecenterrormessage(UTF-16encoded)*/
  56. union{
  57. volatileintisInterrupted;/*Trueifsqlite3_interrupthasbeencalled*/
  58. doublenotUsed1;/*Spacer*/
  59. }u1;
  60. Lookasidelookaside;/*Lookasidemallocconfiguration*/
  61. #ifndefsqlITE_OMIT_AUTHORIZATION
  62. int(*xAuth)(void*,85); line-height:18px"> /*Accessauthorizationfunction*/
  63. void*pAuthArg;/*1stargumenttotheaccessauthfunction*/
  64. #ifndefsqlITE_OMIT_PROGRESS_CALLBACK
  65. int(*xProgress)(void*);/*Theprogresscallback*/
  66. void*pProgressArg;/*Argumenttotheprogresscallback*/
  67. intnProgressOps;/*Numberofopcodesforprogresscallback*/
  68. #endif
  69. #ifndefsqlITE_OMIT_VIRTUALTABLE
  70. HashaModule;/*populatedbysqlite3_create_module()*/
  71. VtabCtx*pVtabCtx;/*Contextforactivevtabconnect/create*/
  72. VTable**aVTrans;/*Virtualtableswithopentransactions*/
  73. intnVTrans;/*AllocatedsizeofaVTrans*/
  74. VTable*pDisconnect;/*Disconnecttheseinnextsqlite3_prepare()*/
  75. FuncDefHashaFunc;/*Hashtableofconnectionfunctions*/
  76. HashaCollSeq;/*Allcollatingsequences*/
  77. BusyHandlerbusyHandler;/*Busycallback*/
  78. intbusyTimeout;/*Busyhandlertimeout,inmsec*/
  79. DbaDbStatic[2];/*Staticspaceforthe2defaultbackends*/
  80. Savepoint*pSavepoint;/*Listofactivesavepoints*/
  81. intnSavepoint;/*Numberofnon-transactionsavepoints*/
  82. intnStatement;/*Numberofnestedstatement-transactions*/
  83. u8isTransactionSavepoint;/*TrueiftheoutermostsavepointisaTS*/
  84. i64nDeferredCons;/*Netdeferredconstraintsthistransaction.*/
  85. int*pnBytesFreed;/*IfnotNULL,incrementthisinDbFree()*/
  86. #ifdefsqlITE_ENABLE_UNLOCK_NOTIFY
  87. /*ThefollowingvariablesareallprotectedbytheSTATIC_MASTER
  88. **mutex,notbysqlite3.mutex.Theyareusedbycodeinnotify.c.
  89. **
  90. **WhenX.pUnlockConnection==Y,thatmeansthatXiswaitingforYto
  91. **unlocksothatitcanproceed.
  92. **
  93. **WhenX.pBlockingConnection==Y,thatmeansthatsomethingthatXtried
  94. **triedtodorecentlyFailedwithansqlITE_LOCKEDerrorduetolocks
  95. **heldbyY.
  96. */
  97. sqlite3*pBlockingConnection;/*ConnectionthatcausedsqlITE_LOCKED*/
  98. sqlite3*pUnlockConnection;/*Connectiontowatchforunlock*/
  99. void*pUnlockArg;/*ArgumenttoxUnlockNotify*/
  100. void(*xUnlockNotify)(void**,int);/*Unlocknotifycallback*/
  101. sqlite3*pNextBlocked;/*Nextinlistofallblockedconnections*/
  102. };

?
    typedefstructsqlite3sqlite3;//

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

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

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

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

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

猜你在找的Sqlite相关文章