An Example SQLite based iPhone Application

2 篇文章 0 订阅
1 篇文章 0 订阅

Link:http://www.techotopia.com/index.php/An_Example_SQLite_based_iPhone_Application


1. Creating the Database and Table

- (void)viewDidLoad {
        NSString *docsDir;
        NSArray *dirPaths;

        // Get the documents directory
        dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        docsDir = [dirPaths objectAtIndex:0];

        // Build the path to the database file
        databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];

        NSFileManager *filemgr = [NSFileManager defaultManager];

        if ([filemgr fileExistsAtPath: databasePath ] == NO)
        {
                const char *dbpath = [databasePath UTF8String];

                if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
                {
                        char *errMsg;
                        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS 
(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";

                        if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
                        {
                                status.text = @"Failed to create table";
                        }

                        sqlite3_close(contactDB);

                } else {
                        status.text = @"Failed to open/create database";
                }
        }

        [filemgr release];
        [super viewDidLoad]; 
}



2. Implementing the Code to Save Data to the SQLite Database

- (void) saveData
{
        sqlite3_stmt    *statement;

        const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {
                NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")", name.text, address.text, phone.text];

                const char *insert_stmt = [insertSQL UTF8String];

                sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
                if (sqlite3_step(statement) == SQLITE_DONE)
                {
                        status.text = @"Contact added";
                        name.text = @"";
                        address.text = @"";
                        phone.text = @"";
                } else {
                        status.text = @"Failed to add contact";
                }
                sqlite3_finalize(statement);
                sqlite3_close(contactDB);
        } 
}



3. Implementing Code to Extract Data from the SQLite Database 

- (void) findContact
{
     const char *dbpath = [databasePath UTF8String];
     sqlite3_stmt    *statement;

     if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
     {
             NSString *querySQL = [NSString stringWithFormat: @"SELECT address, phone FROM contacts WHERE name=\"%@\"", name.text];

             const char *query_stmt = [querySQL UTF8String];

             if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
             {
                     if (sqlite3_step(statement) == SQLITE_ROW)
                     {
                             NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
                             address.text = addressField;

                             NSString *phoneField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
                             phone.text = phoneField;

                             status.text = @"Match found";

                             [addressField release];
                             [phoneField release];
                     } else {
                             status.text = @"Match not found";
                             address.text = @"";
                             phone.text = @"";
                     }
                     sqlite3_finalize(statement);
             }
             sqlite3_close(contactDB);
     } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值