首页 > 代码库 > IOS下SQLite的简单使用

IOS下SQLite的简单使用

本文转载至 http://www.cnblogs.com/cokecoffe/archive/2012/05/31/2537105.html

看着国外网站的教程,写了一个小例子,一个联系人的程序,包括 (姓名、地址、电话)三项内容,通过两个按钮,可以将信息保存或者查询数据库已有的信息。

UI就不说了,比较简单。贴一下关键代码,具体的话还是去看源代码(正想办法传,我这git出点问题)。

 git:https://github.com/cokecoffe/ios-demo/tree/master/SQLite%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8

  1 /*根据路径创建数据库并创建一个表contact(id nametext addresstext phonetext)*/  2   3 - (void)viewDidLoad  4 {  5     [super viewDidLoad];  6     // Do any additional setup after loading the view, typically from a nib.  7       8     NSString *docsDir;  9     NSArray *dirPaths; 10      11     // Get the documents directory 12     dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 13      14     docsDir = [dirPaths objectAtIndex:0]; 15      16     // Build the path to the database file 17     databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]]; 18      19     NSFileManager *filemgr = [NSFileManager defaultManager]; 20      21     if ([filemgr fileExistsAtPath:databasePath] == NO) 22     { 23         const char *dbpath = [databasePath UTF8String]; 24         if (sqlite3_open(dbpath, &contactDB)==SQLITE_OK) 25         { 26             char *errMsg; 27             const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT,PHONE TEXT)"; 28             if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg)!=SQLITE_OK) 29             { 30                 status.text = @"创建表失败\n"; 31             } 32         } 33         else 34         { 35             status.text = @"创建/打开数据库失败"; 36         } 37     } 38      39 } 40  41 /*将数据保存只数据库,当按下保存按钮的时候*/ 42  43 - (IBAction)SaveToDataBase:(id)sender 44 { 45     sqlite3_stmt *statement; 46      47     const char *dbpath = [databasePath UTF8String]; 48      49     if (sqlite3_open(dbpath, &contactDB)==SQLITE_OK) { 50         NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO CONTACTS (name,address,phone) VALUES(\"%@\",\"%@\",\"%@\")",name.text,address.text,phone.text]; 51         const char *insert_stmt = [insertSQL UTF8String]; 52         sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL); 53         if (sqlite3_step(statement)==SQLITE_DONE) { 54             status.text = @"已存储到数据库"; 55             name.text = @""; 56             address.text = @""; 57             phone.text = @""; 58         } 59         else 60         { 61             status.text = @"保存失败"; 62         } 63         sqlite3_finalize(statement); 64         sqlite3_close(contactDB); 65     } 66 } 67  68 /*根据输入的姓名来查询数据*/ 69 - (IBAction)SearchFromDataBase:(id)sender 70 { 71     const char *dbpath = [databasePath UTF8String]; 72     sqlite3_stmt *statement; 73      74     if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 75     { 76         NSString *querySQL = [NSString stringWithFormat:@"SELECT address,phone from contacts where name=\"%@\"",name.text]; 77         const char *query_stmt = [querySQL UTF8String]; 78         if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK) 79         { 80             if (sqlite3_step(statement) == SQLITE_ROW) 81             { 82                 NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)]; 83                 address.text = addressField; 84                  85                 NSString *phoneField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 1    )]; 86                 phone.text = phoneField; 87                  88                 status.text = @"已查到结果"; 89                 [addressField release]; 90                 [phoneField release]; 91             } 92             else { 93                 status.text = @"未查到结果"; 94                 address.text = @""; 95                 phone.text = @""; 96             } 97             sqlite3_finalize(statement); 98         } 99         100         sqlite3_close(contactDB);101     }102 }

IOS下SQLite的简单使用