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
- Install File to Doc dir
- -(BOOL) Installfileofname: (NSString *) FileName {
- NSString *pathfordoc = nil;
- NSString *pathforres = nil;
- Pathfordoc = [Nshomedirectory () stringbyappendingformat:@"/documents/%@", FileName];
- //nslog (@ "%@", Pathfordoc);
- if ([[Nsfilemanager defaultmanager] fileexistsatpath:pathfordoc] = = YES) {
- return NO;
- }Else {
- Pathforres = [[NSBundle mainbundle] pathforresource:filename oftype:[filename pathextension]];
- NSData *filedata = [NSData datawithcontentsoffile:pathforres];
- [[Nsfilemanager Defaultmanager] Createfileatpath:pathfordoc contents:filedata Attributes:nil];
- return YES;
- }
- }
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
- if ([Self installfileofname:@"test_db"] = = YES) {
- NSLog (@"Install database success.");
- }Else {
- NSLog (@"install database fail.");
- }
- NSString *dbpath = [Nshomedirectory () stringbyappendingformat:@"/documents/%@", @"test_db"];
- //nslog (@ "Database path:%@", DbPath);
- Fmdatabase *FM = [Fmdatabase Databasewithpath:dbpath];
- if ([fm open] = = YES) {
- NSLog (@"Open database success.");
- }Else {
- NSLog (@"Open database fail.");
- return;
- }
Next, we can start testing the various SQL statements:
Insert a record into the database:
[CPP]View PlainCopy
- 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
- BOOL operaresult = [FM executeupdate:@] UPDATE test_tab SET name=? Where name=? ", @" 123 ", @" good "];
To delete a record from the database:
[CPP]View PlainCopy
- BOOL operaresult = [fm executeupdate:@"DELETE from Test_tab WHERE name=?", @"Jiajia"];
Get records from the database and sort in descending order:
[CPP]View PlainCopy
- Fmresultset *s = [fm executequery:@"SELECT * from Test_tab ORDER by age DESC"];
Common SQL usage in Fmdb