Ios database sqlite-third-party framework FMDB, thread-safe transaction processing, sqlite-fmdb
Import sqlite3 database and third-party framework FMDB
// 1. Obtain the sandbox path
NSString * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
Path = [path stringByAppendingPathComponent: @ "FMDB. sqlite"];
NSLog (@ "% @", path );
// 2. Create an FMDBDataBaseQueue object and open ()
FMDatabaseQueue * queue = [[FMDatabaseQueue alloc] initWithPath: path];
[Queue inDatabase: ^ (FMDatabase * db ){
NSString * createTable = @ "create table if not exists t_person (person_id integer primary key autoincrement, person_name varchar, person_age integer)"; // creation fails if no primary key exists
BOOL createFlag = [db executeUpdate: createTable];
If (createFlag ){
NSLog (@ "created successfully ");
} Else {
NSLog (@ "creation failed ");
}
}];
/// 3. Add
// [Queue inDatabase: ^ (FMDatabase * db ){
// NSString * insert = @ "insert into t_person (person_name, person_age) values (?,?) ";
// For (int I = 0; I <3; I ++ ){
// NSString * name = [NSString stringWithFormat: @ "jack % I", I];
/// Int age = 20 + I;
// NSString * age = [NSString stringWithFormat: @ "% I", 20 + I];
//
/// NSLog (@ "% @, % @", name, age );
// BOOL insertFlag = [db executeUpdate: insert, name, age];
// If (insertFlag ){
// NSLog (@ "successful data insertion ");
//} Else {
// NSLog (@ "failed to insert data ");
//}
//}
//
//}];
// 4. Modify
[Queue inDatabase: ^ (FMDatabase * db ){
NSString * changeData = @ "update t_person set person_age = 99 where person_id = 3 ";
[Db executeUpdate: changeData];
}];
// 5. Check
[Queue inDatabase: ^ (FMDatabase * db ){
NSString * select = @ "select * from t_person ";
FMResultSet * resultSet = [db executeQuery: select];
}];
// Thread security in the first case to solve the problem of overdraft at the same time (solved above)
// Transfer the money in the second case: transfer the money out, transfer the money in, transfer the money out, but the problem is not resolved to the specified account due to network disconnection or program errors (which can be solved below)
// Create an fmdbDataBase object and open it
FMDatabase * db = [[FMDatabase alloc] initWithPath: path];
[Db open];
// Book buying operation
// 1> deduction
[Db beginDeferredTransaction]; // start the transaction
NSString * minusSql = @ "update t_money set money = money-10 ";
BOOL deletaFlag = [db executeUpdate: minusSql];
NSString * addSql = @ "insert into t_person (person_name, person_age) values ('China', 80 )";
BOOL addFlag = [db executeUpdate: addSql];
// The task is executed only when the execution is successful. If one fails, the task is rolled back"
If (deletaFlag & addFlag ){
[Db commit]; // submit a transaction
NSLog (@ "commit ");
} Else {
[Db rollback]; // roll back the transaction
NSLog (@ "rollback ");
}
}
// Thread security in the first case to solve the problem of overdraft at the same time (solved above)
// Transfer the money in the second case: transfer the money out, transfer the money in, transfer the money out, but the problem is not resolved to the specified account due to network disconnection or program errors (which can be solved below)
// Thread security in the first case to solve the problem of overdraft at the same time (solved above)
// Transfer the money in the second case: transfer the money out, transfer the money in, transfer the money out, but the problem is not resolved to the specified account due to network disconnection or program errors (which can be solved below)
// Thread security in the first case to solve the problem of overdraft at the same time (solved above)
// Transfer the money in the second case: transfer the money out, transfer the money in, transfer the money out, but the problem is not resolved to the specified account due to network disconnection or program errors (which can be solved below)
// Thread security in the first case to solve the problem of overdraft at the same time (solved above)
// Transfer the money in the second case: transfer the money out, transfer the money in, transfer the money out, but the problem is not resolved to the specified account due to network disconnection or program errors (which can be solved below)
// Thread security in the first case to solve the problem of overdraft at the same time (solved above)
// Transfer the money in the second case: transfer the money out, transfer the money in, transfer the money out, but the problem is not resolved to the specified account due to network disconnection or program errors (which can be solved below)