Use SQLite to store data in X-code

Source: Internet
Author: User
Tags sqlite stmt



Development will often encounter some data need to store, if only to store user name or simple user information, there are many ways to store, the simple archive solution can be solved. But when a large amount of data needs to be stored, such as the news or Weibo information that the user has visited, the next time the user opens the app, they want to , you can see what you've browsed, and it's obviously inappropriate to use the archive at this time. PC-side development friends know that data storage has a corresponding database, that iOS development is also a "database" it?



We know that mobile development is very focused on performance, it is not possible to use large databases such as MYSQL,SQLSERVER,DB2 storage data, and SQLite is a lightweight database, just solve the problem, repeat so much, finally to get to the point: How to use SQLite in X-code:



Directly on the code:



First, import a dynamic library: Libsqlite3.dylib






Import Header file:


#import <sqlite3.h>


To create a database:


-(void) viewDidLoad
{
     [super viewDidLoad];
    
     // 1. Get the database file name in the sandbox
     NSString * filename = [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "student.sqlite"];
    
     // 2. Create (open) the database (if the database file does not exist, it will be created automatically)
     int result = sqlite3_open (filename.UTF8String, & _db);
     if (result == SQLITE_OK) {
         NSLog (@ "Successfully opened database");
        
         // 3. Create a table
         const char * sql = "create table if not exists t_student (id integer primary key autoincrement, name text, age integer);";
         char * errorMesg = NULL;
         int result = sqlite3_exec (_db, sql, NULL, NULL, & errorMesg);
         if (result == SQLITE_OK) {
             NSLog (@ "Successfully created t_student table");
         } else {
             NSLog (@ "Failed to create t_student table:% s", errorMesg);
         }
     } else {
         NSLog (@ "Failed to open database");
     }
}
-(IBAction) insert
{
     for (int i = 0; i <30; i ++) {
         NSString * name = [NSString stringWithFormat: @ "Jack-% d", arc4random ()% 100];
         int age = arc4random ()% 100;
         NSString * sql = [NSString stringWithFormat: @ "insert into t_student (name, age) values (‘% @ ‘,% d);", name, age];
        
         char * errorMesg = NULL;
         int result = sqlite3_exec (_db, sql.UTF8String, NULL, NULL, & errorMesg);
         if (result == SQLITE_OK) {
             NSLog (@ "Successfully added data");
         } else {
             NSLog (@ "Failed to add data:% s", errorMesg);
         }
     }
}
-(IBAction) query
{
    // 1.Define the sql statement
    const char * sql = "select id, name, age from t_student where name =?;";
    
    // 2. Define a stmt to store the result set
    sqlite3_stmt * stmt = NULL;
    
    // 3.Check the legitimacy of the SQL statement
    int result = sqlite3_prepare_v2 (_db, sql, -1, & stmt, NULL);
    if (result == SQLITE_OK) {
        NSLog (@ "Query statement is legal");
        
        // Set the contents of the placeholder
        sqlite3_bind_text (stmt, 1, "jack", -1, NULL);
        
        // 4. Execute the SQL statement and fetch the data from the result set
// int stepResult = sqlite3_step (stmt);
        while (sqlite3_step (stmt) == SQLITE_ROW) {// Really query a row of data, here is similar to reader.read () in Ado.net
            // Get the data corresponding to this line
            
            // get the id of column 0
            int sid = sqlite3_column_int (stmt, 0);
            
            // get the name of column 1
            const unsigned char * sname = sqlite3_column_text (stmt, 1);
            
            // get the age of column 2
            int sage = sqlite3_column_int (stmt, 2);
            
            NSLog (@ "% d% s% d", sid, sname, sage);
        }
    } else {
        NSLog (@ "Query statement is not legal");
    }
} 





Use SQLite to store data in X-code


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.