微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlite3增删查改应用

创建一个数据库包括int 类型的id,string 类型的name),并对其进行增删改查

实现步骤:

1.创建一个SingleViewApplication应用。

sqlite3数据库可以采用Mesasqlite可视化工具

2.添加sqlite3支持的库文件,libsqlite3.dylib

3.创建viewController控制器,布局好界面


ViewController.h:

#import <UIKit/UIKit.h>  @interface ViewController : UIViewController - (IBAction)addClick:(id)sender; - (IBAction)deleteClick:(id)sender; - (IBAction)updateClick:(id)sender; - (IBAction)selectClick:(id)sender; @property (retain,nonatomic) IBOutlet UITextField *idText; @property (retain,nonatomic) IBOutlet UITextField *nameText; - (IBAction)viewClick:(id)sender; @property (retain,nonatomic) IBOutlet UISearchBar *txtSearchBar;  @end

ViewController.m:

#import "ViewController.h" #import <sqlite3.h> #define kFilename @"Homeworkdb.rdb"  @interface ViewController ()  @end  @implementation ViewController  - (void)viewDidLoad {     [super viewDidLoad]; 	 }  - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];     // dispose of any resources that can be recreated. }  -(Nsstring *)dataFilePath {     NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);     Nsstring * document = [path objectAtIndex:0];     return [document stringByAppendingPathComponent:kFilename]; }  //增加 - (IBAction)addClick:(id)sender {     //创建     sqlite3 * database;     if(sqlite3_open([[self dataFilePath] UTF8String],&database)!=sqlITE_OK)     {         sqlite3_close(database);         NSAssert(0,@"Failed to open database");     }          Nsstring * creatasql = @"CREATE TABLE IF NOT EXISTS FIELDS" "(id INTEGER PRIMARY KEY,name TEXT);";     char * errorMsg;     if(sqlite3_exec(database,[creatasql UTF8String],NULL,&errorMsg)!=sqlITE_OK)     {         sqlite3_close(database);         NSAssert(0,@"Error creating table:%s",errorMsg);     }              char * update = "insert or replace into FIELDS (id,name)" "values (?,?);";     sqlite3_stmt * stmt;     if(sqlite3_prepare_v2(database,update,-1,&stmt,nil) == sqlITE_OK)     {         sqlite3_bind_int(stmt,1,[self.idText.text intValue]);   //1表示第一个问号         sqlite3_bind_text(stmt,2,[self.nameText.text UTF8String],NULL);//2表示第二个问号     }     if(sqlite3_step(stmt)!=sqlITE_DONE)     {         NSAssert(0,@"Error updating table:%s",errorMsg);     }     sqlite3_finalize(stmt);      //这句话不能漏          sqlite3_close(database);   //关闭数据库          UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"添加成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];     [a show]; }  //删除 - (IBAction)deleteClick:(id)sender {     sqlite3 * database;     if(sqlite3_open([[self dataFilePath] UTF8String],errorMsg);     }                   char * del = "delete from FIELDS where id = ?;";         sqlite3_stmt * stmt;         if(sqlite3_prepare_v2(database,del,nil) == sqlITE_OK)         {             sqlite3_bind_int(stmt,[self.idText.text intValue]);   //1表示第一个问号         }         if(sqlite3_step(stmt)!=sqlITE_DONE)         {             NSAssert(0,@"Error delete table:%s",errorMsg);         }         sqlite3_finalize(stmt);                sqlite3_close(database);          UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"删除成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];     [a show]; }  //修改 - (IBAction)updateClick:(id)sender {     sqlite3 * database;     if(sqlite3_open([[self dataFilePath] UTF8String],errorMsg);     }          char * update = "update FIELDS set id = ?,name = ? where id = ?";     sqlite3_stmt * stmt;     if(sqlite3_prepare_v2(database,NULL);//2表示第二个问号         sqlite3_bind_int(stmt,3,[self.idText.text intValue]);     }     if(sqlite3_step(stmt)!=sqlITE_DONE)     {         NSAssert(0,errorMsg);     }     sqlite3_finalize(stmt);      //这句话不能漏          sqlite3_close(database);   //关闭数据库          UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"修改成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];     [a show]; }  //查询 - (IBAction)selectClick:(id)sender {     //创建数据库     sqlite3 * database;     if(sqlite3_open([[self dataFilePath] UTF8String],errorMsg);     }          Nsstring * query = @"select * from FIELDS where id = ?";     sqlite3_stmt * statement;     if(sqlite3_prepare_v2(database,[query UTF8String],&statement,nil) == sqlITE_OK)     {         sqlite3_bind_int(statement,[self.txtSearchBar.text intValue]);         //sqlite3_bind_text(statement,NULL);         while(sqlite3_step(statement) == sqlITE_ROW)         {             int id = sqlite3_column_int(statement,0);    //0表示第1列             char * name = (char *)sqlite3_column_text(statement,1);  //1表示第2列                          Nsstring * sid = [[Nsstring alloc] initWithFormat:@"%d",id];             Nsstring * sname = [[Nsstring alloc] initWithUTF8String:name];             self.nameText.text = sname;             self.idText.text = sid;             UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"查询成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];             [a show];         }                  sqlite3_finalize(statement);     }     else     {         NSLog(@"error");     }           sqlite3_close(database); }    - (void)dealloc {     [_idText release];     [_nameText release];     [_txtSearchBar release];     [super dealloc]; } - (IBAction)viewClick:(id)sender {     [self.idText resignFirstResponder];     [self.nameText resignFirstResponder]; } @end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐