亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS App項(xiàng)目中引入SQLite數(shù)據(jù)庫的教程

 更新時間:2016年06月02日 09:17:40   作者:劉偉  
SQLite是一個極輕的嵌入式數(shù)據(jù)庫,在應(yīng)用程序中捆綁使用可以更方便地幫助操控關(guān)系型數(shù)據(jù),這里我們就來看一下iOS App項(xiàng)目中引入SQLite數(shù)據(jù)庫的教程

引入SQLite
sqlite是純C實(shí)現(xiàn)的,所以注定了它是一個跨平臺利器,在Android與IOS下均能使用,而且完全可以寫出通用的代碼,方便我們移植。當(dāng)然Android和IOS下都有封裝過的sqlite給開發(fā)者使用,不過這樣子一個是不方便移植,另一個是封裝后的效率咋樣我們也不知道,所以還是原生態(tài)的最健康。最后一個重要的原因就是原生的使用也是相當(dāng)簡單。我將在接下來的教程中為您一一講解。
首先最重要的一點(diǎn)是在工程中導(dǎo)入sqlite,蘋果的SDK已經(jīng)給你包含進(jìn)來了,所以只要導(dǎo)入一個叫 libsqlite3.0.dylib 的 framework就好了。然后,包含相應(yīng)的頭文件:#import "sqlite3.h"    。
在IOS工程的導(dǎo)入就已經(jīng)結(jié)束了,你可以正常使用了。
在其他工程中,比如android中,嵌入式linux中,我們就需要添加兩個文件了 請到 http://sqlite.org/download.html 下載相應(yīng)的文件,你用哪個平臺的就對應(yīng)下哪個文件,不過我一般下第一個叫做 sqlite-amalgamation-3071000.zip  的文件,這個里面包含了一個 sqlite3.c 與一個 sqlite3.h 。我直接把這兩個文件拖到我的工程中去,然后在需要使用的地方把 .h 文件包含進(jìn)來就好了 。這樣比調(diào)用編譯好的庫的好處是我能更方便的調(diào)試,我也能對他的功能做一些修改,比如我可以自己在里面添加一套 自己的加密方式,又或者我可以添加幾個回調(diào)函數(shù)來方便與上層交互。或者刪掉我們不需要的功能,減少代碼冗余。
在我接下來的講解中,我會用純C去講解,雖然我會在蘋果的 xcode 環(huán)境下去寫代碼,但是除了庫的引用方式不一樣以外,其他的都一樣,我會盡量避免與平臺相關(guān)的東西。當(dāng)然有時候我可能會寫一個有UI的Demo,這時候就無可避免地要與平臺打交道了,不過這個教程的關(guān)鍵點(diǎn)在于弄懂底層的原理,學(xué)會sqlite的API的調(diào)用,根據(jù)自己的需求封裝以及提供接口。
最后附上xcode 中導(dǎo)入sqlite的圖:

20166291310645.gif (1024×250)

單擊那個加號。然后搜索sqlite3 ,選取 sqlite3.0.dylib, 然后 單擊Add。然后你就看到工程中這個庫導(dǎo)進(jìn)來了。

20166291339013.gif (390×454)20166291357702.gif (265×228)

然后在需要調(diào)用的地方導(dǎo)入頭文件:

20166291423813.gif (251×42)

SQLite的數(shù)據(jù)類型
要使用數(shù)據(jù)庫你得先弄清楚他的數(shù)據(jù)類型,不是嗎?sqlite 數(shù)據(jù)類型及其簡單:

  • NULL. 空值
  • INTEGER. 整型
  • REAL.浮點(diǎn)型
  • TEXT.文本類型
  • BLOB. 二進(jìn)制類型,用來存儲文件,比如圖片。

以上是sqlite的存儲類型,當(dāng)然,每種類型會根據(jù)數(shù)據(jù)長度有不同的子類型。這個現(xiàn)在不講, 因?yàn)槟憧梢灾苯邮褂蒙鲜鲞@些大的類型。你知道知道有哪幾個類型就好了。以后在實(shí)際運(yùn)用中慢慢熟悉就好了 。

sqlite其實(shí)沒有強(qiáng)制要求你預(yù)先聲明數(shù)據(jù)類型,在實(shí)際存儲過程中它會根據(jù)實(shí)際類型來自動轉(zhuǎn)換,不過為了提高效率我們不建議non-datatype。
下面附上一個創(chuàng)建表的代碼,主要是讓你體會一下數(shù)據(jù)類型的實(shí)際運(yùn)用:

CREATE TABLE student( 
  name TEXT,  //姓名 
  no INTEGER, //學(xué)號 
  totalScore REAL, //總分 
  photo BLOB   //照片 
); 

句柄的定義
要操縱一個數(shù)據(jù)庫你就得有一個這個數(shù)據(jù)庫的句柄(又碰到這個難以理解的詞了,不過確實(shí)還沒得一個更好的詞來替代它)。其實(shí)你跟本不需要去在乎這個詞叫什么,你只要搞清楚他是一個什么玩意兒。就如同鞋子為什么叫鞋子,仔細(xì)想想確實(shí)也難以理解,不過 清楚他的功能就OK了,不是嗎?
句柄在很多地方我們見到過,最常見的就是文件的句柄,我們要操縱一個文件,我們就要取得一個文件的句柄。句柄是個什么東東呢?其實(shí)很簡單,句柄是一個東東的描述,他被定義為一個結(jié)構(gòu)體,這個結(jié)構(gòu)體可能會包含要描述的東東的具體信息,比如位置、大小、類型等等。我們有了這個描述信息我們就能去找到這個東東,然后操縱它。
一句話:句柄是物體的描述結(jié)構(gòu)體。
我們來看看sqlite的句柄是怎么定義的(不要被嚇到了,代碼直接跳過就好):

struct sqlite3 { 
 sqlite3_vfs *pVfs;      /* OS Interface */ 
 int nDb;           /* Number of backends currently in use */ 
 Db *aDb;           /* All backends */ 
 int flags;          /* Miscellaneous flags. See below */ 
 unsigned int openFlags;    /* Flags passed to sqlite3_vfs.xOpen() */ 
 int errCode;         /* Most recent error code (SQLITE_*) */ 
 int errMask;         /* & result codes with this before returning */ 
 u8 autoCommit;        /* The auto-commit flag. */ 
 u8 temp_store;        /* 1: file 2: memory 0: default */ 
 u8 mallocFailed;       /* True if we have seen a malloc failure */ 
 u8 dfltLockMode;       /* Default locking-mode for attached dbs */ 
 signed char nextAutovac;   /* Autovac setting after VACUUM if >=0 */ 
 u8 suppressErr;        /* Do not issue error messages if true */ 
 u8 vtabOnConflict;      /* Value to return for s3_vtab_on_conflict() */ 
 int nextPagesize;       /* Pagesize after VACUUM if >0 */ 
 int nTable;          /* Number of tables in the database */ 
 CollSeq *pDfltColl;      /* The default collating sequence (BINARY) */ 
 i64 lastRowid;        /* ROWID of most recent insert (see above) */ 
 u32 magic;          /* Magic number for detect library misuse */ 
 int nChange;         /* Value returned by sqlite3_changes() */ 
 int nTotalChange;       /* Value returned by sqlite3_total_changes() */ 
 sqlite3_mutex *mutex;     /* Connection mutex */ 
 int aLimit[SQLITE_N_LIMIT];  /* Limits */ 
 struct sqlite3InitInfo {   /* Information used during initialization */ 
  int iDb;          /* When back is being initialized */ 
  int newTnum;        /* Rootpage of table being initialized */ 
  u8 busy;          /* TRUE if currently initializing */ 
  u8 orphanTrigger;      /* Last statement is orphaned TEMP trigger */ 
 } init; 
 int nExtension;        /* Number of loaded extensions */ 
 void **aExtension;      /* Array of shared library handles */ 
 struct Vdbe *pVdbe;      /* List of active virtual machines */ 
 int activeVdbeCnt;      /* Number of VDBEs currently executing */ 
 int writeVdbeCnt;       /* Number of active VDBEs that are writing */ 
 int vdbeExecCnt;       /* Number of nested calls to VdbeExec() */ 
 void (*xTrace)(void*,const char*);    /* Trace function */ 
 void *pTraceArg;             /* Argument to the trace function */ 
 void (*xProfile)(void*,const char*,u64); /* Profiling function */ 
 void *pProfileArg;            /* Argument to profile function */ 
 void *pCommitArg;         /* Argument to xCommitCallback() */   
 int (*xCommitCallback)(void*);  /* Invoked at every commit. */ 
 void *pRollbackArg;        /* Argument to xRollbackCallback() */   
 void (*xRollbackCallback)(void*); /* Invoked at every commit. */ 
 void *pUpdateArg; 
 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64); 
#ifndef SQLITE_OMIT_WAL 
 int (*xWalCallback)(void *, sqlite3 *, const char *, int); 
 void *pWalArg; 
#endif 
 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*); 
 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*); 
 void *pCollNeededArg; 
 sqlite3_value *pErr;     /* Most recent error message */ 
 char *zErrMsg;        /* Most recent error message (UTF-8 encoded) */ 
 char *zErrMsg16;       /* Most recent error message (UTF-16 encoded) */ 
 union { 
  volatile int isInterrupted; /* True if sqlite3_interrupt has been called */ 
  double notUsed1;      /* Spacer */ 
 } u1; 
 Lookaside lookaside;     /* Lookaside malloc configuration */ 
#ifndef SQLITE_OMIT_AUTHORIZATION 
 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*); 
                /* Access authorization function */ 
 void *pAuthArg;        /* 1st argument to the access auth function */ 
#endif 
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK 
 int (*xProgress)(void *);   /* The progress callback */ 
 void *pProgressArg;      /* Argument to the progress callback */ 
 int nProgressOps;       /* Number of opcodes for progress callback */ 
#endif 
#ifndef SQLITE_OMIT_VIRTUALTABLE 
 Hash aModule;         /* populated by sqlite3_create_module() */ 
 VtabCtx *pVtabCtx;      /* Context for active vtab connect/create */ 
 VTable **aVTrans;       /* Virtual tables with open transactions */ 
 int nVTrans;         /* Allocated size of aVTrans */ 
 VTable *pDisconnect;  /* Disconnect these in next sqlite3_prepare() */ 
#endif 
 FuncDefHash aFunc;      /* Hash table of connection functions */ 
 Hash aCollSeq;        /* All collating sequences */ 
 BusyHandler busyHandler;   /* Busy callback */ 
 int busyTimeout;       /* Busy handler timeout, in msec */ 
 Db aDbStatic[2];       /* Static space for the 2 default backends */ 
 Savepoint *pSavepoint;    /* List of active savepoints */ 
 int nSavepoint;        /* Number of non-transaction savepoints */ 
 int nStatement;        /* Number of nested statement-transactions */ 
 u8 isTransactionSavepoint;  /* True if the outermost savepoint is a TS */ 
 i64 nDeferredCons;      /* Net deferred constraints this transaction. */ 
 int *pnBytesFreed;      /* If not NULL, increment this in DbFree() */ 
 
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 
 /* The following variables are all protected by the STATIC_MASTER 
 ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
 ** 
 ** When X.pUnlockConnection==Y, that means that X is waiting for Y to 
 ** unlock so that it can proceed. 
 ** 
 ** When X.pBlockingConnection==Y, that means that something that X tried 
 ** tried to do recently failed with an SQLITE_LOCKED error due to locks 
 ** held by Y. 
 */ 
 sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */ 
 sqlite3 *pUnlockConnection;      /* Connection to watch for unlock */ 
 void *pUnlockArg;           /* Argument to xUnlockNotify */ 
 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */ 
 sqlite3 *pNextBlocked;    /* Next in list of all blocked connections */ 
#endif 
}; 

typedef struct sqlite3 sqlite3;// 

是不是被嚇到了,沒關(guān)系,這段代碼本來就是我貼出來嚇唬你的,我也沒認(rèn)真研究過這個代碼,也不想去研究,除非哪天有人出高價請我去研究,我現(xiàn)在只知道怎么用就好了。
這個 sqlite3 結(jié)構(gòu)體就是被用來描述我們磁盤里的數(shù)據(jù)庫文件的,有了這個描述符我們就可以對這個數(shù)據(jù)庫進(jìn)行各種操作了,操作的具體內(nèi)情我們不必要了解,我們只需要知道怎么去調(diào)用API就好了,當(dāng)然有時候還是要了解一點(diǎn)點(diǎn)內(nèi)情的,這個以后碰到再講。
我用這么長的話加一大段嚇人的代碼只有一個目的:不要對句柄有恐懼感。
好了,開始我們的正題,sqlite 里面你要操縱數(shù)據(jù)庫我們先得創(chuàng)建一個句柄,然后后面所有對數(shù)據(jù)庫得操作都會用到這個句柄。

sqlite3* pdb; 

就 這么簡單,這樣一個ssqlite的句柄我們就創(chuàng)建完成了,我們以后針對數(shù)據(jù)庫的操作都離不開它了。

相關(guān)文章

  • iOS開發(fā) widget構(gòu)建詳解及實(shí)現(xiàn)代碼

    iOS開發(fā) widget構(gòu)建詳解及實(shí)現(xiàn)代碼

    這篇文章主要介紹了iOS開發(fā) widget構(gòu)建詳解的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下
    2016-11-11
  • 比較IOS開發(fā)中常用視圖的四種切換方式

    比較IOS開發(fā)中常用視圖的四種切換方式

    這篇文章給大家介紹了在IOS開發(fā)中常用視圖的四種切換方式以及這四種方式的優(yōu)缺點(diǎn),這四種方式包括:push,pop、modal、切換窗口的根控制器和添加子視圖,有需要的可以參考借鑒。
    2016-08-08
  • iOS中 LGLAlertView 提示框的實(shí)例代碼

    iOS中 LGLAlertView 提示框的實(shí)例代碼

    這篇文章主要介紹了iOS中 LGLAlertView 提示框的實(shí)例代碼非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • 移動web開發(fā)技能之touch事件詳解

    移動web開發(fā)技能之touch事件詳解

    這篇文章主要為大家介紹了移動web開發(fā)技能之touch事件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • IOS開發(fā)環(huán)境windows化攻略

    IOS開發(fā)環(huán)境windows化攻略

    本人主要介紹了IOS開發(fā)環(huán)境windows化攻略,需要的朋友可以參考下
    2013-06-06
  • iOS中修改UISearchBar圓角的小技巧分享

    iOS中修改UISearchBar圓角的小技巧分享

    UISearchBar也是iOS開發(fā)常用控件之一,點(diǎn)進(jìn)去看看里面的屬性barStyle、text、placeholder等等。下面這篇文章主要給大家分享了iOS中修改UISearchBar圓角的小技巧,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。
    2017-05-05
  • iOS常見的幾個修飾詞深入講解

    iOS常見的幾個修飾詞深入講解

    這篇文章主要給大家介紹了關(guān)于iOS常見的幾個修飾詞的相關(guān)資料,iOS修飾詞包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-09-09
  • iOS開發(fā)微信收款到賬語音提醒功能思路詳解

    iOS開發(fā)微信收款到賬語音提醒功能思路詳解

    這篇文章主要介紹了iOS開發(fā)微信收款到賬語音提醒功能思路詳解,需要的朋友可以參考下
    2017-09-09
  • IOS 開發(fā)之Object-C中的對象詳解

    IOS 開發(fā)之Object-C中的對象詳解

    這篇文章主要介紹了IOS 開發(fā)之Object-C中的對象詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 如何使用IOS自動化測試工具UIAutomation

    如何使用IOS自動化測試工具UIAutomation

    這篇文章主要介紹了UIAutomation使用實(shí)例、應(yīng)用技巧、基本知識點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價值
    2021-04-04

最新評論