During this time, the company was developing a new application and used databases. I found some information online and decided to use fmdb. It is easy to use and suitable for people who are new to the database. 1. Step 1: Create a database: (NSString *) filePath :( NSString *) fileName {NSString * pathNSHomeDirectory ();
During this time, the company was developing a new application and used databases. I found some information online and decided to use fmdb. It is easy to use and suitable for people who are new to the database. 1. Step 1: Create a database: (NSString *) filePath :( NSString *) fileName {NSString * path = NSHomeDirectory ();/
During this time, the company was developing a new application and used databases. I found some information online and decided to use fmdb. It is easy to use and suitable for people who are new to the database.
Step 1: Create a database:
+ (NSString *) filePath :( NSString *) fileName
{
NSString * path = NSHomeDirectory ();
// Splicing path Library/Caches
Path = [path stringByAppendingPathComponent: @ "Library/Caches"];
NSLog (@ "path = % @", path );
NSFileManager * fm = [NSFileManager defamanager manager];
// Check whether the specified cache directory exists
If ([fm fileExistsAtPath: path]) {
// Check whether the file name to be saved is legal
If (fileName & [fileName length]! = 0 ){
// Concatenate the full path
Path = [path stringByAppendingPathComponent: fileName];
}
}
Else {
NSLog (@ "the cache directory does not exist ");
}
Return path;
}
-(Id) init
{
DataItem * item = [[DataItem alloc] init];
NSLog (@ "090909090 ==%@", item. destination );
If (self = [super init]) {
// Instantiate a third-party database operation object
// If the user. db file does not exist, create a new one.
// Use it directly if it exists
Fmdb = [[FMDatabase databaseWithPath: [Database filePath: @ "user. db"] retain];
// Try to open the database
If ([fmdb open]) {
// Create a data table
[Self createTable];
}
}
Return self;
}
2. Multiple tables are required to create a database table. Therefore, an array is used to save the table creation statement, and then the table creation statement is executed by traversing the array.
NSArray * reportinfoArray = [NSArray arrayWithObjects: @ "create table if not exists report_info (report_id integer Primary Key Autoincrement, report_name Varchar (1024) default null, user_id integer default null", nil];
The statement for creating a table with report_id as the auto-increment primary node declares it as the primary node. During the table insertion process, the repord_id is returned to auto-increment starting from 1 and does not need to be assigned a value.
For (NSString * SQL in reportinfoArray ){
// Execute the SQL statement
// This method is used to create, add, delete, and modify tables.
If ([fmdb executeUpdate: SQL]) {
NSLog (@ "created ");
}
Else {
NSLog (@ "Table creation failed: % @", [fmdb lastErrorMessage]);
}
}
3. insert one entry or multiple Inserts
-(Void) insertItem :( DataItem *) item
{
If ([self existsItem: item]) {
Return;
} Else {
NSString * SQL = [NSString stringWithFormat: @ "insert into report_info (report_name, user_id, any_invoice, allowance, advance_payment, report_aim, report_starttime, report_endtime, destination) values (?,?,?,?,?,?,?,?,?) "];
// Variable Parameter Method, each? Number indicates a field value. All parameters must be of the object type.
If (SQL ){
[Fmdb executeUpdate: SQL, item. report_name, item. user_id, item. any_invoice, item. allowance, item. advance_payment, item. report_aim, item. report_starttime, item. report_endtime, item. destination];
// Obtain the primary key of the last inserted data
MarkID = [fmdb lastInsertRowId];
}
Else {
NSLog (@ "insertion failed: % @", [fmdb lastErrorMessage]);
}
}
}
Batch insert
-(Void) insertStayArray :( NSArray *) array
{
// Start batch operation
[Fmdb beginTransaction];
For (DataItem * item in array ){
[Self insertStayItem: item];
}
// Submit all modifications
[Fmdb commit];
}
Note: when inserting nsinteger data, the program crashes. The solution is [NSString stringWithFormat: @ "% d", item. report_id] convert it to nsstring class
Type is being inserted.
4. deletion is relatively simple. You only need to delete it based on its primary key according to certain conditions.
-(Void) deletestay_info :( int) stay_id
{
NSString * delete = [NSString stringWithFormat: @ "delete from stay_info WHERE stay_id = % d", stay_id];
BOOL a = [fmdb executeUpdate: delete];
If (){
NSLog (@ "% d, deleted successfully! ", Stay_id );
}
}
5. searching is based on certain conditions.
-(DataItem *) getStay_info :( int) stay_id
{
NSString * SQL = [NSString stringWithFormat: @ "select * from stay_info where stay_id = % d", stay_id];
// Execute the query
FMResultSet * rs = [fmdb executeQuery: SQL];
// If a record exists
DataItem * item = [[DataItem alloc] init] autorelease];
While ([rs next]) {
// This method is a set of methods
// Select different methods based on the field type
Item. stay_id = [rs longForColumn: @ "stay_id"];
Item. stay_endtime = [rs stringForColumn: @ "stay_endtime"];
Item. stay_company_pay = [rs stringForColumn: @ "stay_company_pay"];
}
Return item;
}
6. Modify and determine the conditions to be modified based on the content to be modified
-(Void) alerttraffic_info :( DataItem *) item
{
NSLog (@ "traffic_destination = % @", item. traffic_destination );
NSLog (@ "traffic_id = % d", item. traffic_id );
NSString * Name = [NSString stringWithFormat: @ "UPDATE traffic_info SET traffic_kind = '% @' WHERE traffic_id = % d", item. traffic_kind, item. traffic_id];
[Fmdb executeUpdate: Name];
NSString * trafficDate = [NSString stringWithFormat: @ "UPDATE traffic_info SET traffic_date = '% @ 'where traffic_id = % d", item. traffic_date, item. traffic_id];
[Fmdb executeUpdate: trafficDate];
}