一,導入描述文件
1.
2,
.
3,
二,寫橋接文件sqliteManager
1.文件里寫入
#import <sqlite3.h>就可以了
2.把橋接文件添加到編譯環(huán)境
三, 寫數(shù)據(jù)庫管理類(單例)
import UIKit
class sqliteManager: NSObject {
private static let manager: sqliteManager = sqliteManager()
//單例
class func shareManager() -> sqliteManager{
return manager
}
//數(shù)據(jù)庫對象
private var db:OpaquePointer? = nil
func openDB(sqliteName:String){
//0.拿到數(shù)據(jù)庫的路徑
let path = sqliteName.docDir()
print(path)
let cPath = path.cString(using: String.Encoding.utf8)
//1.需要代開的數(shù)據(jù)庫的路徑 c語言的字符串
//2.打開之后的數(shù)據(jù)庫對象(指針),以后所有的數(shù)據(jù)庫操作,都必須拿到這個指針才能進行相關操作
if sqlite3_open(cPath, &db) != SQLITE_OK{
print("數(shù)據(jù)庫打開失敗")
return
}
if creatTable(){
print("創(chuàng)建表成功")
}else{
print("創(chuàng)建表失敗")
}
}
private func creatTable() -> Bool
{
// 1.編寫SQL語句
// 建議: 在開發(fā)中編寫SQL語句, 如果語句過長, 不要寫在一行
// 開發(fā)技巧: 在做數(shù)據(jù)庫開發(fā)時, 如果遇到錯誤, 可以先將SQL打印出來, 拷貝到PC工具中驗證之后再進行調(diào)試
let sql = "CREATE TABLE IF NOT EXISTS T_Person( \n" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, \n" +
"name TEXT, \n" +
"age INTEGER \n" +
"); \n"
// print(sql)
// 2.執(zhí)行SQL語句
return execSQL(sql: sql)
}
func execSQL(sql: String) -> Bool
{
// 0.將Swift字符串轉換為C語言字符串
let cSQL = sql.cString(using: String.Encoding.utf8)!
// 在SQLite3中, 除了查詢意外(創(chuàng)建/刪除/新增/更新)都使用同一個函數(shù)
/*
1. 已經(jīng)打開的數(shù)據(jù)庫對象
2. 需要執(zhí)行的SQL語句, C語言字符串
3. 執(zhí)行SQL語句之后的回調(diào), 一般傳nil
4. 是第三個參數(shù)的第一個參數(shù), 一般傳nil
5. 錯誤信息, 一般傳nil
*/
if sqlite3_exec(db, cSQL, nil, nil, nil) != SQLITE_OK
{
return false
}
return true
}
}
四,在AppDelegate里調(diào)用openDB函數(shù) 創(chuàng)建數(shù)據(jù)庫
mport UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
sqliteManager.shareManager().openDB(sqliteName: "tergun.sqlite")
return true
}
}
運行結果
附件
工具類
//
// String+Category.swift
// DSWeibo
//
// Created by xiaomage on 15/9/10.
// Copyright © 2015年 小碼哥. All rights reserved.
//
import UIKit
extension String{
/**
將當前字符串拼接到cache目錄后面
*/
func cacheDir() -> String{
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString
return path.appendingPathComponent((self as NSString).lastPathComponent)
}
/**
將當前字符串拼接到doc目錄后面
*/
func docDir() -> String
{
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString
return path.appendingPathComponent((self as NSString).lastPathComponent)
}
/**
將當前字符串拼接到tmp目錄后面
*/
func tmpDir() -> String
{
let path = NSTemporaryDirectory() as NSString
return path.appendingPathComponent((self as NSString).lastPathComponent)
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持html5模板網(wǎng)。
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!