这篇文章主要讲解了“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。