Swift version of SQLite help class, Swift version of SQLite

Source: Internet
Author: User

Swift version of SQLite help class, Swift version of SQLite
SQLiteHelper create SQLiteHelper class

/// SQLite database processing Help class ///// this class encapsulates the business function class SQLiteHelper for SQLite database processing {// business code ...}
Singleton
Private static let instance = SQLiteHelper () // class var sharedInstance: SQLiteHelper {return instance}
Global Variables
var db: COpaquePointer = nil
Open Database
/// Open the database //: param: dbName database name //: returns whether func openDatabase (dbName: String) is successfully enabled) -> Bool {let path = dbName.doc umentPath () println (path) return sqlite3_open (path, & db) = SQLITE_ OK}
Create a data table
/// Create T_Department and T_Employee tables ////: returns whether the func createTable () is successfully created () -> 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 result return execSql (SQL )}
Execute INSERT, UPDATE, and DELETE statements
/// Execute INSERT, UPDATE, and delete SQL statements //: param: SQL SQL statement //: returns: return whether the execution is successful func execSql (SQL: String)-> Bool {// return the result return sqlite3_exec (db, SQL. cStringUsingEncoding (NSUTF8StringEncoding )!, Nil, nil, nil) = ITE_ OK}
Number of results returned by SQL statement execution
/// Number of results returned by executing SQL statements ////: param: SQL SQL statement //: returns: returned func execCount (SQL: String) -> Int {let record = execRecordSet (SQL) // return result return (record [0]! [AnyObject]) [0]! Int}
Execution returns a single record
/// Execute and return a single record //: param: SQL SQL statement //: returns the single record func execRow (SQL: String) -> [AnyObject]? {Let record = execRecordSet (SQL) if record. count> 0 {return (record [0]! [AnyObject])} else {return nil }}
Set of returned SQL results
/// Execution SQL return result set /////: param: SQL SQL statement //: returns: return the query result set 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 )!) } // Release statement sqlite3_finalize (stmt) // return recordList}
Execute a row of data
/// Execute a row of Data /////: param: Statement executed by stmt /////: returns a row of data array func singleData (stmt: COpaquePointer) -> [AnyObject]? {Var result = [AnyObject] () // returns the number of columns in 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 3 for index in 0 .. <count {let type = sqlite3_column_type (stmt, index) // extract the value of switch type for the corresponding Column Based on the field 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> (stmt, index )) let str = String (CString: chars, encoding: NSUTF8StringEncodin G )! Result. append (str) case let type: println ("unsupported type \ (type)")} // return result}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.