Ios database sqlite-third-party framework FMDB, thread-safe transaction processing, sqlite-fmdb

Source: Internet
Author: User

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)


Related Article

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.