IOS database operation fmdb1: used by the FMDB Database
FMDatabase
1. download the src file in the FMDatabase import and download and delete the fmdb. m file 2. introduce framework: libsqlite3.dylib3. introduce the header file # import "FMDatabase. h "database files must be compatible with iPhone 4 and iPhone 5 in the Document or Library directory. First, judge the database files in Document/Library (generally. sqlite suffix). If not, copy the database file from the project (bundle) to the Document or Library.
The following is a database operation code I used in my project. Write it down to avoid forgetting it:[Cpp]View plaincopyprint?
- // Database operation. 1 indicates increase or decrease, 2 indicates deletion, 3 indicates query, 4 indicates modification.
- -(Void) operateDB :( NSInteger) num andRow :( NSInteger) row status :( NSInteger) status {
- Self. deleteOld = YES;
- Self. dbFile = @ "shopCar_db.sqlite ";
- NSFileManager * manager = [NSFileManager defamanager manager];
- NSString * dbPath = [Utilities documentsPath: self. dbFile];
-
- // Copy the object to the document if it does not exist
- If (! [Manager fileExistsAtPath: dbPath]) {
- NSString * bundlePath = [Utilities bundlePath: self. dbFile];
- If ([manager copyItemAtPath: bundlePath toPath: dbPath error: nil]) {
- NSLog (@ "copy OK ...");
- }
- Else
- {
- NSLog (@ "copy error ...");
- }
- }
- Else {
- If (self. deleteOld ){
- NSLog (@ "delete old and create new ..");
- NSString * bundlePath = [Utilities bundlePath: self. dbFile];
- [Manager removeItemAtPath: dbPath error: nil];
- [Manager copyItemAtPath: bundlePath toPath: dbPath error: nil];
- }
- Else
- {
- NSLog (@ "do nothing ...");
- }
-
- }
-
-
- FMDatabase * db = [FMDatabase databaseWithPath: dbPath];
- NSLog (@ "begin to db ...");
- If ([db open]) {
- If (status = 1 ){
- NSLog (@ "add or remove .......");
- }
- If (status = 2 ){
- NSString * SQL = [NSString stringWithFormat: @ "delete from shopCar_tb where id = % I", row];
- [Db executeUpdate: SQL];
- }
- If (status = 3 ){
- NSLog (@ "query ........");
- }
- If (status = 4 ){
- NSString * SQL = [NSString stringWithFormat: @ "UPDATE shopCar_tb SET num = '% d' WHERE id = % d", num, row];
- [Db executeUpdate: SQL];
- }
- NSString * SQL = @ "select * from shopCar_tb ";
- FMResultSet * result = [db executeQuery: SQL];
- _ MutableArrayData = [[NSMutableArray alloc] init];
- [_ MutableArrayData removeAllObjects];
- [_ MutableDictionayData removeAllObjects];
- While ([result next]) {
- _ MutableDictionayData = [[NSMutableDictionary alloc] init];
- NSString * sid = [result stringForColumn: @ "id"];
- [_ MutableDictionayData setObject: sid forKey: @ "id"];
-
- NSString * name = [result stringForColumn: @ "name"];
- [_ MutableDictionayData setObject: name forKey: @ "name"];
-
- NSString * gid = [result stringForColumn: @ "gid"];
- [_ MutableDictionayData setObject: gid forKey: @ "gid"];
-
- NSString * num = [result stringForColumn: @ "num"];
- [_ MutableDictionayData setObject: num forKey: @ "num"];
- NSLog (@ "num = % I", [num intValue]);
-
- NSString * price = [result stringForColumn: @ "price"];
- [_ MutableDictionayData setObject: price forKey: @ "price"];
-
- NSString * path = [result stringForColumn: @ "path"];
- [_ MutableDictionayData setObject: path forKey: @ "path"];
-
- [_ MutableArrayData addObject: _ mutableDictionayData];
- }
-
- }
- [Db close];
-
- NSLog (@ "end db ...");
-
-
- }