Import Foundation/**1. Open the Database2. If there is no data table, you need to first create a table3. Data operation */Class SQLite {var db:copaquepointer = Nil ///Open Database /// ///:p Aram:dbname Database name /// ///: Returns: whether to open successfullyFunc OpenDatabase (dbname:string), Bool { //Unsafepointer<int8> unsafepointer<cchar> //corresponding to char* in C language //FileName must be the full path name Let path = Dbname.documentpath () println (PATH) //Sqlite3_open If the database does not exist, a new database file is created //If the database file already exists, open it directly and return the handle without any effect on the data If Sqlite3_open (path, &db) = = SQLITE_OK { println"Open Database Succeeded ") //Essentially, it only needs to be run once. If CreateTable () { println"Success of the Record ") //TODO: Test Query data Let sql ="Select ID, Departmentno, Name from T_department; " RecordSet (SQL) }else { println"Failed to create a table ") } }else { println"Failed to open database ") } ReturnFalse} ///Create a data table, create a data table that the system needs, once Private Func createtable (), Bool { //Prepare SQL for all data tables //1> each SQL is completed with one; //2> Write all the Genesis SQL together, adding a \ n for each line break Let sql ="CREATE TABLE \ n "+ "IF not EXISTS t_department (\ n "+ "ID INTEGER PRIMARY KEY autoincrement not null,\n "+ "Departmentno CHAR (Ten) not NULL DEFAULT ' ', \ n ' + "Name CHAR (a) 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 is not NULL, \ n ' + "' department_id ' INTEGER, \ n ' + "CONSTRAINT ' fk_dep_id ' FOREIGN KEY (' department_id ') REFERENCES ' t_department ' (' ID ') \ n "+ ");" return Execsql (SQL)} ///Execute SQL statement with no return value /// ///:p Aram:sql SQL string /// ///: Returns: whether successfulFunc Execsql (sql:string), Bool { /** 1. Database pointers 2. C language format for SQL strings 3. Callback, function callback after the completion of the SQL command, usually nil 4. A pointer to the first parameter of the callback 5. Error messages, usually also passing nil */ Return sqlite3_exec (DB, sql.cstringusingencoding (nsutf8stringencoding)!, nil, nil, nil) = = Sqlite_ok} ///Execute SQL Returns a result set (object array) /// ///:p Aram:sql SQL stringFunc RecordSet (sql:string) { //1. Prepare the statement var stmt:copaquepointer = Nil /** 1. Database handle 2. SQL's C-language string 3. The string length of the C language of SQL strlen,-1 is automatically calculated 4. Pointers to stmt 5. Usually pass in nil */ If SQLITE3_PREPARE_V2 (db, Sql.cstringusingencoding (nsutf8stringencoding)!,-1, &stmt, nil) = = SQLITE_OK { //Single-Step Get SQL Execution results-Sqlite3_setup corresponds to a record While Sqlite3_step (stmt) = = Sqlite_row { //Get the data for each record RecordData (stmt) } }} ///Get a record of each piece of data /// ///:p aram:stmt prepared_statement ObjectsFunc RecordData (stmt:copaquepointer) { //Get to record var count = Sqlite3_column_count (stmt) println"Get to record, total number of columns \ (count) ") //Traverse data for each column For I in0.. <count { Let type = Sqlite3_column_type (stmt, i) //Extracts the value of the corresponding column based on the type of the field Switch type { Case Sqlite_integer: println"Integer \ (Sqlite3_column_int64 (stmt, i)) Case Sqlite_float: println"Small tree \ (sqlite3_column_double (stmt, i)) Case Sqlite_null: println"Empty \ (NSNull ()) ") Case Sqlite_text: let chars = Unsafepointer<cchar> (Sqlite3_column_text (stmt, i)) Span class= "indent" > let str = String (cstring:chars, encoding: nsutf8stringencoding)! println ( " string \ (str)") case let type: println ( " Span class= "indent" >} }}}
Using Sqlite3 in Swift