這篇文章主要講解了“Swift版的SQLite幫助類怎么創(chuàng)建”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Swift版的SQLite幫助類怎么創(chuàng)建”吧!
成都創(chuàng)新互聯(lián)公司專注于湘橋網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供湘橋營(yíng)銷型網(wǎng)站建設(shè),湘橋網(wǎng)站制作、湘橋網(wǎng)頁(yè)設(shè)計(jì)、湘橋網(wǎng)站官網(wǎng)定制、小程序設(shè)計(jì)服務(wù),打造湘橋網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供湘橋網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
/// SQLite數(shù)據(jù)庫(kù)處理幫助類 /// /// 此類中封裝了關(guān)于SQLite數(shù)據(jù)庫(kù)處理的業(yè)務(wù)函數(shù) class SQLiteHelper { // 業(yè)務(wù)代碼...}
private static let instance = SQLiteHelper()/// 單例 全局的數(shù)據(jù)訪問(wèn)接口class var sharedInstance: SQLiteHelper {return instance }
var db: COpaquePointer = nil
/// 打開(kāi)數(shù)據(jù)庫(kù)////// :param: dbName 數(shù)據(jù)庫(kù)名稱////// :returns: 返回 是否打開(kāi)成功func openDatabase(dbName: String) -> Bool {let path = dbName.documentPath() println(path)return sqlite3_open(path, &db) == SQLITE_OK }
/// 創(chuàng)建 T_Department 和 T_Employee 表////// :returns: 返回 是否創(chuàng)建成功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" + ");" // 返回結(jié)果 return execSql(sql) }
/// 執(zhí)行INSERT、UPDATE、DELETE SQL語(yǔ)句////// :param: sql SQL語(yǔ)句////// :returns: 返回 是否執(zhí)行成功func execSql(sql: String) -> Bool {// 返回結(jié)果 return sqlite3_exec(db, sql.cStringUsingEncoding(NSUTF8StringEncoding)!, nil, nil, nil) == ITE_OK }
/// 執(zhí)行SQL語(yǔ)句 返回結(jié)果數(shù)量////// :param: sql SQL語(yǔ)句////// :returns: 返回 結(jié)果func execCount(sql: String) -> Int {let record = execRecordSet(sql)// 返回結(jié)果return (record[0] as! [AnyObject])[0] as! Int }
/// 執(zhí)行返回單條記錄////// :param: sql SQL語(yǔ)句////// :returns: 返回 單條記錄func execRow(sql: String) -> [AnyObject]? {let record = execRecordSet(sql)if record.count > 0{return (record[0] as! [AnyObject]) }else{return nil } }
/// 執(zhí)行 SQL 返回結(jié)果集合////// :param: sql SQL語(yǔ)句////// :returns: 返回 查詢的結(jié)果集func execRecordSet(sql: String) -> [AnyObject] {var stmt: COpaquePointer = nilvar 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)!) } }// 釋放語(yǔ)句sqlite3_finalize(stmt)// 返回結(jié)果return recordList }
/// 執(zhí)行一行數(shù)據(jù)////// :param: stmt 執(zhí)行的語(yǔ)句////// :returns: 返回一行數(shù)據(jù)數(shù)組func singleData(stmt: COpaquePointer) -> [AnyObject]? {var result = [AnyObject]()// 返回該表的列數(shù)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 3for index in 0..= sqlite3_column_text(stmt, index)let chars = UnsafePointer (sqlite3_column_text(stmt, index))let str = String(CString: chars, encoding: NSUTF8StringEncoding)! result.append(str)case let type: println("不支持的類型 \(type)") } }// 返回結(jié)果return result }
感謝各位的閱讀,以上就是“Swift版的SQLite幫助類怎么創(chuàng)建”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Swift版的SQLite幫助類怎么創(chuàng)建這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!