注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

熊猫正正的博客

熊猫正正的天空

 
 
 

日志

 
 

SQLite数据库实例  

2013-04-14 22:55:29|  分类: ios开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
今天学习了一下IOS中的数据存储,利用sqlite来实现数据的存取,发出来与大家共同学习!
将libsqlite3.0.dylib,libsqlite3.dylib加入到项目中
ViewController.h代码如下:

#import <UIKit/UIKit.h>

#import <sqlite3.h>


@interface ViewController : UIViewController

{

    sqlite3 *contactDB;

    

    UITextField *name;

    UITextField *address;

    UITextField *phone;

    

    UILabel *status;

    NSString *databasePath;

}


@property (retain,nonatomic) IBOutlet UITextField *name;

@property (retain,nonatomic) IBOutlet UITextField *address;

@property (retain,nonatomic) IBOutlet UITextField *phone;

@property (retain,nonatomic) IBOutlet UILabel *status;


- (IBAction) SaveToDataBase :(id)sender;

- (IBAction) SearchFromDataBase :(id)sender;

- (IBAction) backgroundTap:(id)sender;


@end




ViewController.m代码如下:

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


@synthesize name;

@synthesize address;

@synthesize phone;

@synthesize status;


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    NSString *docsDir;

    NSArray *dirPaths;

    

    //Get the documents directory

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    

    docsDir = [dirPaths objectAtIndex: 0];

    

    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 = @"创建表失败\n";

            }

        }

        else

        {

            status.text = @"创建/打开数据库失败";

        }

    }

}


- (IBAction)SaveToDataBase:(id)sender

{

    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 = @"已存储到数据库";

            name.text = @"";

            address.text = @"";

            phone.text = @"";

        }

        else

        {

            status.text = @"保存失败";

        }

        sqlite3_finalize(statement);

        sqlite3_close(contactDB);

    }

}

- (IBAction)SearchFromDataBase:(id)sender

{

    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 = @"已查到结果";

            }

            else {

                status.text = @"未查到结果";

                address.text = @"";

                phone.text = @"";

            }

            sqlite3_finalize(statement);

        }

        

        sqlite3_close(contactDB);

    }

}


//关闭键盘

- (IBAction) backgroundTap:(id)sender

{

    [name resignFirstResponder];

    [phone resignFirstResponder];

    [address resignFirstResponder];

}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


  评论这张
 
阅读(58)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017