標籤:sqlite swift
SQLiteHelper建立SQLiteHelper類
/// SQLite資料庫處理協助類////// 此類中封裝了關於SQLite資料庫處理的業務函數class SQLiteHelper{ // 業務代碼...}
單例
private static let instance = SQLiteHelper()/// 單例 全域的資料提供者class var sharedInstance: SQLiteHelper{ return instance}
全域變數
var db: COpaquePointer = nil
開啟資料庫
/// 開啟資料庫////// :param: dbName 資料庫名稱////// :returns: 返回 是否開啟成功func openDatabase(dbName: String) -> Bool{ let path = dbName.documentPath() println(path) return sqlite3_open(path, &db) == SQLITE_OK}
建立樣本資料表
/// 建立 T_Department 和 T_Employee 表////// :returns: 返回 是否建立成功func createTable() -> Bool{ let sql = "CREATE TABLE \n" + "IF NOT EXISTS T_Department (\n" + "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" + "DepartmentNo CHAR(10) NOT NULL DEFAULT ‘‘,\n" + "Name CHAR(50) NOT NULL DEFAULT ‘‘ \n" + "); \n" + "CREATE TABLE IF NOT EXISTS T_Employee ( \n" + "‘id‘ INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \n" + "‘name‘ TEXT NOT NULL, \n" + "‘age‘ INTEGER NOT NULL, \n" + "‘department_id‘ INTEGER, \n" + "CONSTRAINT ‘FK_DEP_ID‘ FOREIGN KEY (‘department_id‘) REFERENCES ‘T_Department‘ (‘id‘) \n" + ");" // 返回結果 return execSql(sql)}
執行INSERT、UPDATE、DELETE 語句
/// 執行INSERT、UPDATE、DELETE SQL語句////// :param: sql SQL語句////// :returns: 返回 是否執行成功func execSql(sql: String) -> Bool{ // 返回結果 return sqlite3_exec(db, sql.cStringUsingEncoding(NSUTF8StringEncoding)!, nil, nil, nil) == ITE_OK}
執行SQL語句 返回結果數量
/// 執行SQL語句 返回結果數量////// :param: sql SQL語句////// :returns: 返回 結果func execCount(sql: String) -> Int{ let record = execRecordSet(sql) // 返回結果 return (record[0] as! [AnyObject])[0] as! Int}
執行返回單條記錄
/// 執行返回單條記錄////// :param: sql SQL語句////// :returns: 返回 單條記錄func execRow(sql: String) -> [AnyObject]?{ let record = execRecordSet(sql) if record.count > 0 { return (record[0] as! [AnyObject]) } else { return nil }}
執行 SQL 返回結果集合
/// 執行 SQL 返回結果集合////// :param: sql SQL語句////// :returns: 返回 查詢的結果集func execRecordSet(sql: String) -> [AnyObject]{ var stmt: COpaquePointer = nil var recordList = [AnyObject]() if sqlite3_prepare_v2(db, sql.cStringUsingEncoding(NSUTF8StringEncoding)!, -1, &stmt, nil) == TE_OK { while sqlite3_step(stmt) == SQLITE_ROW { recordList.append(singleData(stmt)!) } } // 釋放語句 sqlite3_finalize(stmt) // 返回結果 return recordList}
執行一行資料
/// 執行一行資料////// :param: stmt 執行的語句////// :returns: 返回一行資料數組func singleData(stmt: COpaquePointer) -> [AnyObject]?{ var result = [AnyObject]() // 返回該表的列數 let count = sqlite3_column_count(stmt) // #define SQLITE_INTEGER 1 // #define SQLITE_FLOAT 2 // #define SQLITE_BLOB 4 // #define SQLITE_NULL 5 // #ifdef SQLITE_TEXT // # undef SQLITE_TEXT // #else // # define SQLITE_TEXT 3 // #endif // #define SQLITE3_TEXT 3 for index in 0..<count { let type = sqlite3_column_type(stmt, index) // 根據欄位的類型,提取對應列的值 switch type { case SQLITE_INTEGER: result.append(Int(sqlite3_column_int64(stmt, index))) case SQLITE_FLOAT: result.append(sqlite3_column_double(stmt, index)) case SQLITE_NULL: result.append(NSNull()) case SQLITE_TEXT: let rrrrr: UnsafePointer<UInt8> = sqlite3_column_text(stmt, index) let chars = UnsafePointer<CChar>(sqlite3_column_text(stmt, index)) let str = String(CString: chars, encoding: NSUTF8StringEncoding)! result.append(str) case let type: println("不支援的類型 \(type)") } } // 返回結果 return result}
Swift版的SQLite協助類