The operation method of SQLite in IOS _ios

Source: Internet
Author: User
Tags sqlite stmt

Today finally spent a little time on the previous project used in the SQLite operation of the method to organize and upload the github, download the address: (Https://github.com/peanutNote/QYSQLiteManagerDemo.git).

As with other third-party purposes, the main purpose is to make the code relevant to the SQLite operation simple, specific use:

Add the Qysqlitemanager file to your project, adding #import "Qysqlitemanager" to the class that you want to operate on SQLite.

//INSERT statement-(void) inserttable {//Create SQL statement NSString *sql = @ "INSERT into teacher (Name,id) VALUES (
 ?,?)";
 Immutable parameters//BOOL IsOK = [Qysqlitemanager inserttablewithsqlstring:sql andarray:@[@ "Xiaoming", @115]];
 Variable parameter BOOL IsOK = [Qysqlitemanager inserttablewithsqlstring:sql andobjects:@ "xiaoming", @ "to be", nil];
 if (IsOK) {NSLog (@ "data inserted successfully");
 else {NSLog (@ "Data insert failed");
 }//Query statement-(void) selecttable {nsstring *sql = @ "SELECT * from teacher"; [Qysqlitemanager selecttablewithsqlstring:sql didfinishedblock:^ (Nsarray *datalist, NSString *error)
 {NSLog (@ "%@", dataList);
} Andobjects:nil];
 }//Modify table statement-(void) altertable {nsstring *sql = @ "ALTER TABLE teacher add column pwd integer";
 if ([Qysqlitemanager Altertablewithsqlstring:sql]) {NSLog (@ "modified successfully");
 }//Update data statement-(void) updatetable {nsstring *sql = @ "Update teacher set name =? where id =?";
 if ([Qysqlitemanager updatetablewithsqlstring:sql andarray:@[@ "Xiaoming", @115]]) {NSLog (@ "update succeeded"); }
}

For the data type returned by the query, students who need it can find "Sqlite3_bind_text" in "qysqlitemanager.m" on their own, and then in the following

for (int i = 0; i < Column_count i++) {
     //Get field name
     char * keyname = (char *) sqlite3_column_name (stmt, i);
     NSString *key = [NSString stringwithutf8string:keyname];
     if (Sqlite3_column_type (stmt, i) = = Sqlite_text) {///When the field data is ' TEXT '/
      /get the corresponding data
      char *valuename = (char *) sqlite3 _column_text (stmt, i);
      NSString *value = [NSString stringwithutf8string:valuename];
      [Datadic Setobject:value Forkey:key];
     } else {//When field data is integer
      int value = Sqlite3_column_int (stmt, i);
      [Datadic setobject:@ (value) Forkey:key];
     }
    

Modify the data type you want.

The above content is small make up to everybody daily collects and arranges the iOS SQLite to the database all sorts of operation, hope to be helpful to everybody

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.