iOS中sqlite數(shù)據(jù)庫的原生用法
在iOS中,也同樣支持sqlite。目前有很多第三方庫,封裝了sqlite操作,比如swift語言寫的SQLite.swift、蘋果官網(wǎng)也為我們封裝了一個框架:CoreData。
它們都離不開Sqlite數(shù)據(jù)庫的支持。
本文主要介紹下,如何在swift中使用原生的sqlite的API。
在Xcode中引入sqlite API
新建一個swift項目后,我們需要讓項目引入sqlite的動態(tài)鏈接庫:
1、項目配置界面,選擇Build Phases

2、點開Link Binary With Libraries,點擊+號,在彈窗中輸入sqlite3

完成后:

3、創(chuàng)建橋接文件,在項目目錄下新建一個頭文件(h):

4、然后使用import導入sqlite庫:
#import "sqlite3.h"

5、最后一步,在項目配置界面,選擇Build Setting,搜索框中輸入swift,在結果中選擇Objective-C Bridging Header,輸入剛才新建的橋接文件的名稱:

整個引入工作已經(jīng)完成了,可以測試下是否引入成功,在swift文件中,輸入sqlite3,看是否有sqlite3相關的智能提示出來。
創(chuàng)建(打開)與關閉數(shù)據(jù)庫
要創(chuàng)建或者打開一個sqlite數(shù)據(jù)庫,使用sqlite3_open方法,我們無需手動創(chuàng)建一個數(shù)據(jù)庫文件,如果沒有文件,sqlite3_open方法會為我們自動創(chuàng)建數(shù)據(jù)庫文件,然后打開數(shù)據(jù)庫。
//數(shù)據(jù)庫存放路徑
let sqlitepath = NSHomeDirectory().stringByAppendingPathComponent("Documents/sqlite3.db")
//打開數(shù)據(jù)庫,指定數(shù)據(jù)庫文件路徑,如果文件不存在后先創(chuàng)建文件,再打開,所以無需手動創(chuàng)建文件
let state = sqlite3_open(sqlitepath, &db)
if state == SQLITE_OK{
println("打開數(shù)據(jù)庫成功")
}else{
println("打開數(shù)據(jù)庫失敗")
}
這里sqlite3_open方法的第二個參數(shù)是一個指針,是數(shù)據(jù)庫打開后返回的一個操作指針,通過使用它,我們可以對數(shù)據(jù)庫進行一系列的操作。我們先把它定義在外面,方便我們使用。
var db:COpaquePointer = nil
override func viewDidLoad() {
super.viewDidLoad()
...
}
創(chuàng)建表和刪除表
使用sqlite3_exec方法可以執(zhí)行一段sql語句,主要就是sql語句的差異,其他都一樣:
//創(chuàng)建表
let createtable = "create table if not exists students (id integer primary key autoincrement,name
text,stuId integer)"
let result = sqlite3_exec(db, createtable, nil, nil, nil)
if result == SQLITE_OK{
println("創(chuàng)建表成功")
}
//刪除表
let removetable = "drop table studets"
let result2 = sqlite3_exec(db, removetable, nil, nil, nil)
if result2 == SQLITE_OK{
println("刪除表成功")
}
插入更新刪除數(shù)據(jù)
插入數(shù)據(jù)使用了占位符,sql語句中使用問號代表值。使用sqlite3_bind_xxx來綁定值。
//插入數(shù)據(jù)
let inserttable = "insert into studets (stuId,name) values(?,?)"
var statement:COpaquePointer = nil
let result3 = sqlite3_prepare_v2(db, inserttable, -1, &statement, nil)
if result3 == SQLITE_OK{
//綁定數(shù)據(jù)
sqlite3_bind_int(statement, 1, 1)
sqlite3_bind_text(statement, 2, "lijialong", -1, nil)
//執(zhí)行
if sqlite3_step(statement) == SQLITE_DONE{
println("數(shù)據(jù)插入成功")
}
sqlite3_finalize(statement)
}
更新數(shù)據(jù)也類似幾個步驟:
let updatetable = "update studets set name ='ss' where id = 1" var statement:COpaquePointer = nil sqlite3_prepare_v2(db, updatetable, -1, &statement, nil) sqlite3_step(statement) sqlite3_finalize(statement)
刪除數(shù)據(jù):
let deleterow = "delete from studets where name='lijialong'" sqlite3_prepare_v2(db, deleterow, -1, &statement, nil) sqlite3_step(statement) sqlite3_finalize(statement)
查詢數(shù)據(jù)
//查詢數(shù)據(jù)
let query = "select * from studets"
//這條執(zhí)行后,數(shù)據(jù)就已經(jīng)在sattement中了
sqlite3_prepare_v2(db, query, -1, &statement, nil)
//游標往下走一步,如果返回SQLITE_ROW就進入
while sqlite3_step(statement) == SQLITE_ROW{
let id = sqlite3_column_int(statement, 0)
let stuId = sqlite3_column_int(statement, 2)
}
以上就是關于iOS中sqlite數(shù)據(jù)庫的原生用法詳細介紹,希望對大家的學習有所幫助。
- iOS開發(fā)中使用SQL語句操作數(shù)據(jù)庫的基本用法指南
- iOS中FMDB數(shù)據(jù)庫之增刪改查使用實例
- iOS sqlite對數(shù)據(jù)庫的各種操作(日常整理全)
- iOS App項目中引入SQLite數(shù)據(jù)庫的教程
- iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫
- iOS學習筆記(十六)——詳解數(shù)據(jù)庫操作(使用FMDB)
- 詳解ios中的SQL數(shù)據(jù)庫文件加密 (使用sqlcipher)
- iOS App使用SQLite之句柄的定義及數(shù)據(jù)庫的基本操作
- IOS 數(shù)據(jù)庫升級數(shù)據(jù)遷移的實例詳解
- iOS開發(fā)中如何優(yōu)雅的調(diào)試數(shù)據(jù)庫詳解
相關文章
IOS 開發(fā)之UITableView 刪除表格單元寫法
這篇文章主要介紹了IOS 開發(fā)之UITableView 刪除表格單元寫法的相關資料,這里提供實例幫助大家實現(xiàn)該功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08
iOS開發(fā)-調(diào)用系統(tǒng)相機和相冊獲取照片示例
這篇文章主要介紹了iOS開發(fā)-調(diào)用系統(tǒng)相機和相冊獲取照片示例的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
iOS App開發(fā)中使cell高度自適應的黑魔法詳解
這篇文章主要介紹了iOS App開發(fā)中使cell高度自適應的黑魔法詳解,作者利用iOS8以后的新特性講解了TableView、CollectionView中的cell高度自適應以及UITextView輸入內(nèi)容實時更新cell高度的方法,需要的朋友可以參考下2016-03-03

