swift3.0 創(chuàng)建sqlite數(shù)據(jù)庫步驟方法
更新時間:2017年06月30日 08:32:44 作者:tergun
本篇文章主要介紹了swift3.0 創(chuàng)建sqlite數(shù)據(jù)庫步驟方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
一,導(dǎo)入描述文件
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ù)庫操作,都必須拿到這個指針才能進行相關(guān)操作
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字符串轉(zhuǎn)換為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
}
}
運行結(jié)果

附件
工具類
//
// String+Category.swift
// DSWeibo
//
// Created by xiaomage on 15/9/10.
// Copyright © 2015年 小碼哥. All rights reserved.
//
import UIKit
extension String{
/**
將當(dāng)前字符串拼接到cache目錄后面
*/
func cacheDir() -> String{
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString
return path.appendingPathComponent((self as NSString).lastPathComponent)
}
/**
將當(dāng)前字符串拼接到doc目錄后面
*/
func docDir() -> String
{
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString
return path.appendingPathComponent((self as NSString).lastPathComponent)
}
/**
將當(dāng)前字符串拼接到tmp目錄后面
*/
func tmpDir() -> String
{
let path = NSTemporaryDirectory() as NSString
return path.appendingPathComponent((self as NSString).lastPathComponent)
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- DJango的創(chuàng)建和使用詳解(默認(rèn)數(shù)據(jù)庫sqlite3)
- Python操作SQLite數(shù)據(jù)庫的方法詳解【導(dǎo)入,創(chuàng)建,游標(biāo),增刪改查等】
- C#操作SQLite數(shù)據(jù)庫方法小結(jié)(創(chuàng)建,連接,插入,查詢,刪除等)
- PHP+sqlite數(shù)據(jù)庫操作示例(創(chuàng)建/打開/插入/檢索)
- Android創(chuàng)建和使用數(shù)據(jù)庫SQLIte
- android創(chuàng)建數(shù)據(jù)庫(SQLite)保存圖片示例
- SQLite 創(chuàng)建數(shù)據(jù)庫實例操作
相關(guān)文章
Compose聲明式代碼語法對比React?Flutter?SwiftUI
這篇文章主要為大家介紹了Compose聲明式代碼語法對比React?Flutter?SwiftUI來解釋為什么說?Compose?的聲明式代碼最簡潔,有需要的朋友可以借鑒參考下2022-08-08
Swift設(shè)置UILabel內(nèi)邊距的實例代碼
有時候,我們需要一個顯示文字,又想這些文字與邊界之間有自定義的邊距,所以下面這篇文章主要給大家介紹了關(guān)于Swift設(shè)置UILabel內(nèi)邊距的相關(guān)資料,需要的朋友可以參考下2021-10-10

