sqlite3增删查改应用

前端之家收集整理的这篇文章主要介绍了sqlite3增删查改应用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

实现步骤:

1.创建一个SingleViewApplication应用。

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

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

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

@H_403_19@@H_502_20@


@H_403_19@@H_502_20@ViewController.h:

@H_403_19@@H_502_20@
#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

@H_403_19@@H_502_20@ViewController.m:

@H_403_19@@H_502_20@
#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
原文链接:https://www.f2er.com/sqlite/201093.html

猜你在找的Sqlite相关文章