IOS下SQLite的簡單使用


看着國外網站的教程,寫了一個小例子,一個聯系人的程序,包括 (姓名、地址、電話)三項內容,通過兩個按鈕,可以將信息保存或者查詢數據庫已有的信息。

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 }

 

 

Wangkeke 2012-05-31 23:37 發表評論


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM