IOS Fmdatabase Local database creation and several basic usage methods

Source: Internet
Author: User

Transferred from: http://blog.it985.com/3677.html

Before using the database, of course, download the Fmdb library online and add it to your project. No, please click below to download
Fmdb
Add "Libsqulite3.0.dylib" to the framework, or the library will cause a lot of error when it is put in.

In general, we have built an application database in the sandbox of the current program, so we need to get the sandbox path first.
In the APPDELEGATE.M

123456 - (NSString *) dataFilePath//应用程序的沙盒路径{    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *document = [path objectAtIndex:0];    return[document stringByAppendingPathComponent:@"StudentData.sqlite"];}

If the database is also used in other files, set the path to a global variable after obtaining the sandbox path
In the AppDelegate.h

1 @property (strong, nonatomic) NSString *dbPath;

In the APPDELEGATE.M

12345 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];    myDelegate.dbPath = [self dataFilePath];    returnYES;}

Once the path is ready, the next step is to create the database and table locally.
Database statements are basically easy to read literally
In the APPDELEGATE.M

123456789101112131415161718 - (void)createTable{    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];    NSFileManager *fileManager = [NSFileManager defaultManager];    FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];    if (![fileManager fileExistsAtPath:myDelegate.dbPath]) {        NSLog(@"还未创建数据库,现在正在创建数据库");        if ([db open]) {                        [db executeUpdate:@"create table if not exists StudentList (name text, address text, id text)"];                        [db close];        }else{            NSLog(@"database open error");        }    }    NSLog(@"FMDatabase:---------%@",db);}

In this way, we create a database named "DB", and a table named "Studentlist".
It is important to note that if you want to use a database after you have created it, make sure that [db Open] is used before the end of use [db Close]. This is never to be forgotten.
And then we're going to use the library in other. m files as follows
If you want to insert a new set of data into a table

1234567891011121314 AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];[db open];NSString *name = @"苹果";NSString *address = @"安徽";int i = 1;NSString *id = [NSString stringWithFormat:@"%d",i];res = [db executeUpdate:@"INSERT INTO StudentList (name, address, id) VALUES (?, ?, ?)", name, address, id];if (res == NO) {    NSLog(@"数据插入失败");    }else{     NSLog(@"数据插入成功");    }[db close];

Modify the database (change the address and name of ID 1)

1 res = [db executeUpdate:@"UPDATE StudentList SET name = ?, address = ? WHERE id = ?",@"橘子",@"芜湖",1];

Querying the database (name and address with a query ID of 1)

12 NSString *nameOut = [db stringForQuery:@"SELECT name FROM StudentList WHERE id = ?",1];NSString *addressOut=  [db stringForQuery:@"SELECT address FROM StudentList WHERE id = ?",1];

Delete a database (delete data with ID 1)

1 res = [db executeUpdate:@"DELETE FROM StudentList WHERE id = ?",1];

Explain the above "res" is the detection of errors in the flag bit, if you do not want to use it. Also, if you want to add integer data to the database, you may get an error, and it is recommended that you convert the integer to a string and add it as follows.

123 inti = 1;NSString *id = [NSString stringWithFormat:@"%d",i];res = [db executeUpdate:@"INSERT INTO StudentList (name, address, id) VALUES (?, ?, ?)", name, address, id];

Permanent address of this article: http://blog.it985.com/3677.html
This article is from IT985 blog, please indicate the source and corresponding link when reproduced.

IOS Fmdatabase Local database creation and several basic usage methods

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.