Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create the SQLite help class for Swift

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "Swift version of SQLite help class how to create", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Swift version of SQLite help class how to create" it!

SQLiteHelper create SQLiteHelper class / SQLite database processing help class / this class encapsulates the business function class SQLiteHelper {/ / business code about SQLite database processing.} singleton private static let instance = SQLiteHelper () / / Singleton global data access interface class var sharedInstance: SQLiteHelper {return instance} global variable var db: COpaquePointer = nil Open Database / Open Database / /: param: dbName database name /: returns: returns whether func openDatabase (dbName: String) is opened successfully-> Bool {let path = dbName.documentPath () println (path) return sqlite3_open (path) & db) = = SQLITE_OK} create sample data tables / / create T_Department and T_Employee tables /: returns: return whether 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 'arrangement' ('id')\ n "+") "/ / return result return execSql (sql)} execute INSERT, UPDATE, DELETE statement / / execute INSERT, UPDATE, DELETE SQL statement /: param: sql SQL statement /: returns: return whether func execSql (sql: String) has been successfully executed-> Bool {/ / return result return sqlite3_exec (db, sql.cStringUsingEncoding (NSUTF8StringEncoding)!, nil, nil Nil) = = ITE_OK} number of results returned by executing SQL statement / number of results returned by executing SQL statement /: param: sql SQL statement /: returns: return result func execCount (sql: String)-> Int {let record = execRecordSet (sql) / / return result return (record [0] as! [AnyObject]) [0] as! Int} execute return single record / / execute return single record /: param: sql SQL statement /: returns: return single record func execRow (sql: String)-> [AnyObject]? {let record = execRecordSet (sql) if record.count > 0 {return (record [0] as! [AnyObject])} else {return nil}} execute SQL return result set / / execute SQL return result set /: param: sql SQL statement /: returns: return query result set 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)!)}} / / release statement sqlite3_finalize (stmt) / / return result return recordList} execute a row of data / execute a row of data /: param: stmt execute a statement /: returns: return a row According to the array func singleData (stmt: COpaquePointer)-> [AnyObject]? {var result = [AnyObject] () / / returns the number of columns of the table 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..

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report