IOS development-database-sqlite operation 2

Source: Internet
Author: User

1. Create a single view project, import the FMDatabase library, and import the libsqlite3 system library;

2. ViewController. xib File


3. ViewController. h

@interface ViewController : UIViewController
 
  {    IBOutlet UITextField *_name;    IBOutlet UIImageView *_image;    FMDatabase *_db;}- (IBAction)add:(id)sender;- (IBAction)del:(id)sender;- (IBAction)search:(id)sender;- (IBAction)update:(id)sender;
 

4. ViewController. m

-(Void) viewDidLoad {[super viewDidLoad]; // _ image. userInteractionEnabled = YES; // UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (selectImage)]; // tap. numberOfTapsRequired = 2; // double-click the trigger. By default, click // tap. numberOfTouchesRequired = 2; // press Both fingers at the same time. The default value is [_ image addGestureRecognizer: tap]; [tap release]; // database path NSString * path = [NSString stringWit HFormat: @ "% @/Documents/data. db ", NSHomeDirectory ()]; _ db = [[FMDatabase alloc] initWithPath: path];}-(void) selectImage {UIImagePickerController * ipc = [[UIImagePickerController alloc] init]; ipc. sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // set the image source to ipc. delegate = self; [self presentViewController: ipc animated: YES completion: nil]; // use the modal view to display the selection window [ipc release];} // select image call-(void) image PickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info {_ image. image = [info objectForKey: Custom]; [self dismissViewControllerAnimated: YES completion: nil];} // click Cancel call-(void) Cancel :( UIImagePickerController *) picker {[self dismissViewControllerAnimated: YES completion: nil];}-(IBAction) add :( id) sender {If (_ name. text = nil | _ image. image = nil) {NSLog (@ "null"); return;} // open the database BOOL res = [_ db open]; if (res = NO) {NSLog (@ "failed to open"); return;} // open successfully // create table res = [_ db executeUpdate: @ "create table if not exists studentss (id integer primary key autoincrement, name, image)"]; if (res = NO) {NSLog (@ "table creation failed "); [_ db close]; return;} // NSNumber NSString NSData // insert NSData * data = UIImagePNGReprese Ntation (_ image. image); res = [_ db executeUpdate: @ "insert into studentss (name, image) values (?,?) ", _ Name. text, data]; if (res = NO) {NSLog (@ "insertion failed");} [_ db close];}-(IBAction) del :( id) sender {BOOL res = [_ db open]; if (res = NO) {NSLog (@ "failed to open"); return;} res = [_ db executeUpdate: @ "delete from studentss where name =? ", _ Name. text]; if (res = NO) {NSLog (@ "delete failed");} [_ db close];}-(IBAction) search :( id) sender {BOOL res = [_ db open]; if (res = NO) {NSLog (@ "failed to open"); return;} FMResultSet * set = [_ db executeQuery: @ "select * from studentss"]; while ([set next]) {int uid = [set intForColumn: @ "id"]; // obtain the field name. // int uid = [set intForColumnIndex: 0]; NSString * name = [set stringForColumnIndex: 1] based on the field index. // NSData * data = [Set dataForColumn: @ "image"]; NSLog (@ "% d --- % @", uid, name) ;}[ _ db close] ;}- (IBAction) update :( id) sender {BOOL res = [_ db open]; if (res = NO) {NSLog (@ "failed to open"); return ;} res = [_ db executeUpdate: @ "update studentss set name = 'bala' where name =? ", _ Name. text]; if (res = NO) {NSLog (@" update failed ");} [_ db close];}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.