现在的位置: 首页 > 综合 > 正文

IOS sqlit3的基本使用

2017年02月26日 ⁄ 综合 ⁄ 共 4343字 ⁄ 字号 评论关闭

/*

 数据库操作的基本步骤

 1、创建数据库

 2、创建数据库表

 3、插入数据到数据库

 4、查询数据库

 */

先用xcode工具建立一个简单的工程。

简历xib界面

RootViewController.h文件的内容为:

#import <UIKit/UIKit.h>

#import <sqlite3.h>

@interface RootViewController :
UIViewController

{

   
sqlite3 *contactDB;   //数据库

   
NSString *databasePath; //数据库文件的路径

}

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

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

@property (weak,
nonatomic) IBOutlet
UITextField *telephone;

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

- (IBAction)SaveDataSql:(UIButton *)sender;

- (IBAction)SearchDataFromSql:(UIButton *)sender;

@end

RootViewController.m文件的内容为:

//

//  RootViewController.m

//  sqlite3基本练习

//

//  Created by qin on 14-3-29.

//  Copyright (c) 2014 zhou. All rights reserved.

//

#import "RootViewController.h"

@interface
RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   
self = [super
initWithNibName:nibNameOrNil
bundle
:nibBundleOrNil];

   
if (self) {

        // Custom initialization

    }

    return
self;

}

/*

 数据库操作的基本步骤

 1、创建数据库

 2、创建数据库表

 3、插入数据到数据库

 4、查询数据库

 */

- (void)viewDidLoad

{

    [super
viewDidLoad];

    /*一、创建数据库*/

    //1、得到document目录下的所有对象

   
NSArray *dirPaths;

   
NSString *docsDir;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);

    //1、得到document目录下的0位置的对象

    docsDir = [dirPaths
objectAtIndex:0];

    //3、建立数据库文件的路径 databasePath = docsDir/contacts.db

    databasePath = [[NSString
alloc]initWithString:[docsDir
stringByAppendingPathComponent:@"contacts.db"]];

    //4、创建一个文件管理器来查看文件

    NSFileManager *filemgr = [NSFileManager
defaultManager];

    //5、查看数据库
没有就创建

   
if([filemgr fileExistsAtPath:databasePath] ==
NO)

    {

        //文件名要是char *型的则需要转换

       
const char  *filename = [databasePath
UTF8String];

        //第一个参数为char *的文件名
第二个参数为数据库的地址

//        if(sqlite3_open(<#const char *filename#>, <#sqlite3 **ppDb#>))

       
if(sqlite3_open(filename, &contactDB) ==
SQLITE_OK)

        {

            //6、为数据库创建一个表

        //第一个参数为数据库
第二个为数据库表的指针

//            sqlite3_exec(<#sqlite3 *#>, <#const char *sql#>, <#int (*callback)(void *, int, char **, char **)#>, <#void *#>, <#char **errmsg#>)

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

            }

        }

       
else

        {

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

        }

    }

}

- (void)didReceiveMemoryWarning

{

    [super
didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

//保存数据库

- (IBAction)SaveDataSql:(UIButton *)sender

{

    sqlite3_stmt *statement;
//插入信息句柄
保存数据库插入的情况
主要的一些标志

   
const char *filename = [databasePath
UTF8String];

   
if(sqlite3_open(filename, &contactDB) ==
SQLITE_OK)

    {

        NSString *insertSQL = [NSString
stringWithFormat:@"INSERT INTO CONTACTS(name,address,phone)VALUES(\"%@\",\"%@\",\"%@\")",_name.text,_address.text,_telephone.text];

       
const char *insert_stmt = [insertSQL
UTF8String];

       
//插入数据库

       
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement,
NULL);

        if(sqlite3_step(statement) ==
SQLITE_DONE)
//sqlite3_step遍历select DONE表示执行完毕

        {

           
_status.text =
@"已存到数据库";

           
_name.text =
@"";

           
_address.text =
@"";

           
_telephone.text =
@"";

        }

       
else

        {

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

        }

       
sqlite3_finalize(statement);

        sqlite3_close(contactDB);

    }

}

//查询数据

- (IBAction)SearchDataFromSql:(UIButton *)sender

{

   
sqlite3_stmt *statement;

   
const char *filename = [databasePath
UTF8String];

   
if(sqlite3_open(filename, &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)
//sqlite3_step遍历select SQLITE_ROW表示执行完毕还有返回

            {

               
NSString *addressField = [[NSString
alloc]initWithUTF8String :( const
char *)sqlite3_column_text(statement,
0)];//text类型的数据

               
NSLog(@"地址为%@",addressField);

               
_address.text = addressField;

 

               
NSString *phoneField = [[NSString
alloc]initWithUTF8String :( const
char *)sqlite3_column_text(statement,
1)];//text类型的数据

               
NSLog(@"地址为%@",phoneField);

               
_telephone.text = phoneField;

                

               
_status.text =
@"已查到结果";

            }

           
else

            {

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

               
_address.text =
@"";

               
_telephone.text =
@"";

            }

           
sqlite3_finalize(statement);

        }

        sqlite3_close(contactDB);

    }

}

@end

运行结果为:

抱歉!评论已关闭.