Common SQL usage in Fmdb

Source: Internet
Author: User

Everyone in the work, the most commonly used is to increase, delete, check, change ...

The corresponding statement in SQL is: INSERT DELETE SELECT UPDATE

First, you can visualize the management database using a tool called "SQLite database Browser".

Then add the database file to the resource directory, and then install the database using the following code, which is copied to the doc directory:

[CPP]View PlainCopy
  1. Install File to Doc dir
  2. -(BOOL) Installfileofname: (NSString *) FileName {
  3. NSString *pathfordoc = nil;
  4. NSString *pathforres = nil;
  5. Pathfordoc = [Nshomedirectory () stringbyappendingformat:@"/documents/%@", FileName];
  6. //nslog (@ "%@", Pathfordoc);
  7. if ([[Nsfilemanager defaultmanager] fileexistsatpath:pathfordoc] = = YES) {
  8. return NO;
  9. }Else {
  10. Pathforres = [[NSBundle mainbundle] pathforresource:filename oftype:[filename pathextension]];
  11. NSData *filedata = [NSData datawithcontentsoffile:pathforres];
  12. [[Nsfilemanager Defaultmanager] Createfileatpath:pathfordoc contents:filedata Attributes:nil];
  13. return YES;
  14. }
  15. }

The above method, you can call directly, pass a database file name, then it will copy it to the doc directory.

Here are some Fmdb operations to install and open the database:

[CPP]View PlainCopy
  1. if ([Self installfileofname:@"test_db"] = = YES) {
  2. NSLog (@"Install database success.");
  3. }Else {
  4. NSLog (@"install database fail.");
  5. }
  6. NSString *dbpath = [Nshomedirectory () stringbyappendingformat:@"/documents/%@", @"test_db"];
  7. //nslog (@ "Database path:%@", DbPath);
  8. Fmdatabase *FM = [Fmdatabase Databasewithpath:dbpath];
  9. if ([fm open] = = YES) {
  10. NSLog (@"Open database success.");
  11. }Else {
  12. NSLog (@"Open database fail.");
  13. return;
  14. }


Next, we can start testing the various SQL statements:

Insert a record into the database:

[CPP]View PlainCopy
    1. BOOL operaresult = [fm executeupdate:@"INSERT into Test_tab (name,age,mail) VALUES (?,?,?)" @"good" @"4" @  "[email protected]"];

To modify a record in a database:

[CPP]View PlainCopy
    1. BOOL operaresult = [FM executeupdate:@] UPDATE test_tab SET name=?  Where name=? ", @" 123 ", @" good "];


To delete a record from the database:

[CPP]View PlainCopy
    1. BOOL operaresult = [fm executeupdate:@"DELETE from Test_tab WHERE name=?", @"Jiajia"];


Get records from the database and sort in descending order:

[CPP]View PlainCopy
      1. Fmresultset *s = [fm executequery:@"SELECT * from Test_tab ORDER by age DESC"];

Common SQL usage in Fmdb

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.