SQLite additions and deletions to search

Source: Internet
Author: User
Tags sqlite stmt

//Basic Steps//1. Open the database//2. Processing Data//3. Close the database 
// first SET global variable lazy var documentspath:string = {        true)        return paths.first!     } ()    // pointer    var db:opaquepointer? = Nil    var stmt: Opaquepointer? = Nil
//Create or open a databasefunc createoropendatabase () {print ("\ (nshomedirectory ())")                //Create the database file path and change to the UTF-8 typeLet path:nsstring ="\ (documentspath)/test.sqlite3"  asnsstring let filename=path.utf8string//determines whether the open database is successful (automatically generated if no database is created), does not succeed print the input "Create or Open failed ..." and closes the database        ifSqlite3_open (filename, &db)! =SQLITE_OK {print ("Create or Open failed ...") sqlite3_close (db)}}//Create student Tablesfunc createtable () {//splicing SQL statements and turning to UTF-8Letstring: NSString ="CREATE table if not exists Student (ID integer primary key autoincrement, sno text, name text, score integer)"Let SQL=string. utf8string//Execute SQL statement        ifSQLITE3_EXEC (DB, SQL, nil, nil, nil)! =SQLITE_OK {print ("CREATE TABLE failed ...") sqlite3_close (db)}}//new operations for SLQfunc insertstudent (sno:string, name:string, score:int) {//Preparing SQL statementsLetstring: NSString ="INSERT INTO Student (SNO, name, score) VALUES (?,?,?)"Let SQL=string. utf8string//Parsing SQL text statements//SQLITE3_PREPARE_V2 parsing//parameter 1: current database pointer//parameter 2: SQL statement to parse (ends with 0 by default)//Parameter 3: Because the SQL statement ends with 0 by default, in order to go out that 0, it is -1//parameter 4: Another pointer is used for parsing and storing, and the last parameter is forgotten, you can write nil directly.        ifSQLITE3_PREPARE_V2 (DB, SQL,-1, &stmt, nil)! =SQLITE_OK {sqlite3_close (db) Print ("\ (Sno), insert failed ...")        }                //Binding ParametersLet Csno = (sno asnsstring). utf8string Let CNAME= (name asnsstring). Utf8string Sqlite3_bind_text (stmt,1, Csno,-1, nil) sqlite3_bind_text (stmt,2, CNAME,-1, nil) sqlite3_bind_int (stmt,3, Int32 (score))//Execute SQL statement        ifSqlite3_step (stmt) = =sqlite_error {sqlite3_close (db) Print ("\ (Sno), insert failed ...")        } Else {            //Freeing Resourcessqlite3_finalize (stmt)}}//Modify student Datafunc updatestudent () {//Preparing SQL statementsLetstring: NSString ="Update Student Set score = + where name like ' a% '"Let SQL=string. utf8string//Execute SQL statement//sqlite3_exec The following three parameters can not be closed, write nil on it.        ifSQLITE3_EXEC (DB, SQL, nil, nil, nil)! =SQLITE_OK {sqlite3_close (db) Print ("update failed ...")        }    }        //Delete student Datafunc deletestudents () {//Preparing SQL statementsLetstring: NSString ="Delete from Student where score <"Let SQL=string. utf8string//Execute SQL statement        ifSQLITE3_EXEC (DB, SQL, nil, nil, nil)! =SQLITE_OK {sqlite3_close (db) Print ("Delete Failed ...")        }    }
Summary:Additions and deletions (if no parameters, then the 2nd, 3 steps do not have to write)1: Preparing SQL statementssuch as: let string:nsstring = "INSERT into Student (SNO, name, score) VALUES (?,?,?)"2: Parse SQL statements such as: SQLITE3_PREPARE_V2 (DB, SQL,-1, &stmt, nil)3: Binding Parameterssuch as: let Csno = (sno as NSString). Utf8stringSqlite3_bind_text (stmt, 1, Csno,-1, nil)4 execution statements such as: Sqlite3_step (stmt)5: Releasing resources such as: Sqlite3_finalize (stmt)  Enquiry1: Preparing SQL statements2: Parsing SQL statements3: Execute SQL statementget data for each record (some character types also need to be converted to display the results you want)Sqlite3_column_text (stmt, 0)4: Freeing up resources

SQLite additions and deletions to search

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.