[Code Note] iOS-FMDBDemo, note ios-fmdbdemo

Source: Internet
Author: User

[Code Note] iOS-FMDBDemo, note ios-fmdbdemo

I ,.

2. Engineering Drawing.

3. Code.

ViewController. h

#import <UIKit/UIKit.h>#import "FMDatabase.h"#import "FMDatabaseQueue.h"@interface ViewController : UIViewController{    FMDatabase *db;    NSString *database_path;    }@end

 

ViewController. m

# Import "ViewController. h "# define DBNAME @" personinfo. sqlite "# define ID @" id "# define NAME @" name "# define AGE @" age "# define ADDRESS @" address "# define TABLENAME @" PERSONINFO "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // initialize the database storage directory [self addDocumentPath]; // The initialization interface [self AddView]; [super viewDidLoad] ;}# pragma-mark-functions // initialize the database storage directory-(void) addDocumentPath {// obtain the Documents directory NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documents = [paths objectAtIndex: 0]; NSLog (@ "-- documents -- % @", documents); // Add, make it a complete path database_path = [documents stringByAppendingPathComponent: DBNAME]; NSLog (@ "-- database_path -- -% @ ", Database_path); db = [FMDatabase databaseWithPath: database_path];} // initialize the user interface-(void) addView {// create a database UIButton * createBtn = [UIButton buttonWithType: UIButtonTypeRoundedRect]; createBtn. frame = CGRectMake (60, 60,200, 50); [createBtn addTarget: self action: @ selector (doClickCreateButton) forControlEvents: UIControlEventTouchUpInside]; [createBtn setTitle: @ "createTable" forState: UIControlStateNormal]; [s Elf. view addSubview: createBtn]; // Insert the database UIButton * insterBtn = [UIButton buttonWithType: UIButtonTypeRoundedRect]; insterBtn. frame = CGRectMake (60,130,200, 50); [insterBtn addTarget: self action: @ selector (doClickInsertButton) forControlEvents: role]; [insterBtn setTitle: @ "insert" forState: UIControlStateNormal]; [self. view addSubview: insterBtn]; // update the database UIButton * updateBtn = [UIButto N buttonWithType: UIButtonTypeRoundedRect]; updateBtn. frame = CGRectMake (60,200,200, 50); [updateBtn addTarget: self action: @ selector (doClickUpdateButton) forControlEvents: Events]; [updateBtn setTitle: @ "update" forState: UIControlStateNormal]; [self. view addSubview: updateBtn]; // Delete the database UIButton * deleteBtn = [UIButton buttonWithType: UIButtonTypeRoundedRect]; deleteBtn. frame = CGRectMak E (60,270,200, 50); [deleteBtn addTarget: self action: @ selector (doClickDeleteButton) forControlEvents: UIControlEventTouchUpInside]; [deleteBtn setTitle: @ "delete" forState: UIControlStateNormal]; [self. view addSubview: deleteBtn]; // view the database UIButton * selectBtn = [UIButton buttonWithType: UIButtonTypeRoundedRect]; selectBtn. frame = CGRectMake (60,340,200, 50); [selectBtn addTarget: self action: @ selector (doCl IckSelectButton) forControlEvents: UIControlEventTouchUpInside]; [selectBtn setTitle: @ "select" forState: UIControlStateNormal]; [self. view addSubview: selectBtn]; // multithreading UIButton * multithreadBtn = [UIButton buttonWithType: UIButtonTypeRoundedRect]; multithreadBtn. frame = CGRectMake (60,410,200, 50); [multithreadBtn addTarget: self action: @ selector (doClickMultithreadButton) forControlEvents: UIControlEventTo UchUpInside]; [multithreadBtn setTitle: @ "multithread" forState: UIControlStateNormal]; [self. view addSubview: multithreadBtn] ;}# pragma-mark-doClickAction // create a database-(void) doClickCreateButton {// SQL statement if ([db open]) {NSString * sqlCreateTable = [NSString stringWithFormat: @ "create table if not exists '% @' ('% @ 'integer primary key autoincrement,' % @ 'TEXT, '% @ 'integer,' % @ 'text) ", TABLENAME, ID, NAME, AGE, ADDRESS]; BOOL res = [db executeUpdate: sqlCreateTable]; if (! Res) {NSLog (@ "error when creating db table");} else {NSLog (@ "success to creating db table");} [db close];} // INSERT database-(void) doClickInsertButton {if ([db open]) {NSString * insertSql1 = [NSString stringWithFormat: @ "insert into '% @' ('% @', '% @', '% @') VALUES ('% @', '% @', '% @') ", TABLENAME, NAME, AGE, ADDRESS, @ "James", @ "13", @ "Jinan"]; BOOL res = [db executeUpdate: insertSql1]; if (! Res) {NSLog (@ "error when insert db table");} else {NSLog (@ "success to insert db table");} NSString * insertSql2 = [NSString stringWithFormat: @ "insert into '% @' ('% @', '% @', '% @') VALUES ('% @', '% @', '% @') ", TABLENAME, NAME, AGE, ADDRESS, @" Li Si ", @" 12 ", @" Jinan "]; BOOL res2 = [db executeUpdate: insertSql2]; if (! Res2) {NSLog (@ "error when insert db table");} else {NSLog (@ "success to insert db table");} [db close];} // modify the database-(void) doClickUpdateButton {if ([db open]) {NSString * updateSql = [NSString stringWithFormat: @ "UPDATE '% @ 'set' % @' % = '% @ 'where' % @' = '% @'", TABLENAME, AGE, @ "15", AGE, @ "13"]; BOOL res = [db executeUpdate: updateSql]; if (! Res) {NSLog (@ "error when update db table");} else {NSLog (@ "success to update db table");} [db close];} // Delete the database-(void) doClickDeleteButton {if ([db open]) {NSString * deleteSql = [NSString stringWithFormat: @ "delete from % @ where % @ = '% @'", TABLENAME, NAME, @ "James"]; BOOL res = [db executeUpdate: deleteSql]; if (! Res) {NSLog (@ "error when delete db table");} else {NSLog (@ "success to delete db table");} [db close];} // view database-(void) doClickSelectButton {if ([db open]) {NSString * SQL = [NSString stringWithFormat: @ "SELECT * FROM % @", TABLENAME]; FMResultSet * rs = [db executeQuery: SQL]; while ([rs next]) {int Id = [rs intForColumn: ID]; NSString * name = [rs stringForColumn: NAME]; NSString * age = [rs stri NgForColumn: AGE]; NSString * address = [rs stringForColumn: ADDRESS]; NSLog (@ "id = % d, name = % @, age = % @ address = % @", id, name, age, address);} [db close] ;}// multi-threaded operation database-(void) doClickMultithreadButton {FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath: database_path]; dispatch_queue_t q1 = dispatch_queue_create ("queue1", NULL); dispatch_queue_t q2 = dispatch_queue_create ("queue2 ", NULL); dispatch_async (q1, ^ {for (int I = 0; I <50; ++ I) {[queue inDatabase: ^ (FMDatabase * db2) {NSString * insertSql1 = [NSString stringWithFormat: @ "insert into '% @' ('% @', '% @', '% @') VALUES (?, ?, ?) ", TABLENAME, NAME, AGE, ADDRESS]; NSString * name = [NSString stringWithFormat: @" jack % d ", I]; NSString * age = [NSString stringWithFormat: @ "% d", 10 + I]; BOOL res = [db2 executeUpdate: insertSql1, name, age, @ "Jinan"]; if (! Res) {NSLog (@ "error to inster data: % @", name);} else {NSLog (@ "succ to inster data: % @", name) ;}}] ;}}); dispatch_async (q2, ^ {for (int I = 0; I <50; ++ I) {[queue inDatabase: ^ (FMDatabase * db2) {NSString * insertSql2 = [NSString stringWithFormat: @ "insert into '% @' ('% @', '% @', '% @') VALUES (?, ?, ?) ", TABLENAME, NAME, AGE, ADDRESS]; NSString * name = [NSString stringWithFormat: @" lilei % d ", I]; NSString * age = [NSString stringWithFormat: @ "% d", 10 + I]; BOOL res = [db2 executeUpdate: insertSql2, name, age, @ "Beijing"]; if (! Res) {NSLog (@ "error to inster data: % @", name);} else {NSLog (@ "succ to inster data: % @", name) ;}}] ;}}) ;}- (void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} @ end

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.