您好,登錄后才能下訂單哦!
這篇文章主要講解了“Swift版的SQLite幫助類怎么創建”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Swift版的SQLite幫助類怎么創建”吧!
/// 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 SQL語句////// :param: sql SQL語句////// :returns: 返回 是否執行成功func execSql(sql: String) -> Bool {// 返回結果 return sqlite3_exec(db, sql.cStringUsingEncoding(NSUTF8StringEncoding)!, nil, nil, nil) == ITE_OK }
/// 執行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 返回結果集合////// :param: sql SQL語句////// :returns: 返回 查詢的結果集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)!) } }// 釋放語句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 3for 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幫助類怎么創建”的內容了,經過本文的學習后,相信大家對Swift版的SQLite幫助類怎么創建這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。