"code note" Ios-fmdbdemo

Source: Internet
Author: User
Tags uikit
<span id="Label3"></p><p><p><span style="font-size: 18px">One.</span></p></p><p><p><span style="font-size: 18px"></span></p></p><p><p><span style="font-size: 18px">two, Engineering Drawings.</span></p></p><p><p><span style="font-size: 18px"></span></p></p><p><p><span style="font-size: 18px">three, Code.</span></p></p><p><p><span style="font-size: 18px">ViewController.h</span></p></p><span class="cnblogs_code_copy" style="font-size: 18px"><span class="cnblogs_code_copy" style="font-size: 18px"></span></span><pre><pre><span style="font-size: 18px"><span style="color: #0000ff">#import <UIKit/UIKit.h><span style="color: #0000ff"><span style="color: #800000">"<span style="color: #800000">FMDatabase.h<span style="color: #800000">"<span style="color: #0000ff"><span style="color: #800000">"<span style="color: #800000">FMDatabaseQueue.h<span style="color: #800000">"<span style="color: #0000ff">@interface <span style="color: #000000"> Viewcontroller:uiviewcontroller{fmdatabase *<span style="color: #000000">db; NSString *<span style="color: #000000">database_path;} <span style="color: #0000ff">@end</span> </span> </span> </span> </span> </span> </span> </span></span> </span> </span> </span></span></span></span></pre></pre><span class="cnblogs_code_copy" style="font-size: 18px"><span class="cnblogs_code_copy" style="font-size: 18px"></span></span><p><p><span style="font-size: 18px"></span></p></p><p><p><span style="font-size: 18px">Viewcontroller.m</span></p></p><span class="cnblogs_code_copy" style="font-size: 18px"><span class="cnblogs_code_copy" style="font-size: 18px"></span></span><pre><span style="font-size: 18px"><span style="color: #0000ff">#import<span style="color: #800000">"<span style="color: #800000">ViewController.h<span style="color: #800000">"<span style="color: #0000ff">#define DBNAME @ "personinfo.sqlite"<span style="color: #0000ff">#define ID @ "id"<span style="color: #0000ff">#define NAME @ "name"<span style="color: #0000ff">#define Age @ ' age '<span style="color: #0000ff">#define ADDRESS @ "address"<span style="color: #0000ff">#define TABLENAME @ "PERSONINFO"<span style="color: #0000ff">@interface<span style="color: #000000">Viewcontroller ()<span style="color: #0000ff">@end<span style="color: #0000ff">@implementation<span style="color: #000000">viewcontroller-(<span style="color: #0000ff">void<span style="color: #000000">) Viewdidload {[super viewdidload];<span style="color: #008000">//<span style="color: #008000">Additional setup after loading the view, typically from a nib.<span style="color: #008000">//<span style="color: #008000">Initializing the database storage directory<span style="color: #000000">[self adddocumentpath];<span style="color: #008000">//<span style="color: #008000">Initializing the interface<span style="color: #000000">[self addview]; [super viewdidload];}<span style="color: #0000ff">#pragma-mark-functions<span style="color: #008000">//<span style="color: #008000">Initialize the database storage directory-(<span style="color: #0000ff">void<span style="color: #000000">) adddocumentpath{<span style="color: #008000">//<span style="color: #008000">Get documents directory Nsarray *paths =<span style="color: #000000">Nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, YES); NSString *documents = [paths objectatindex:<span style="color: #800080">0<span style="color: #000000">]; NSLog (<span style="color: #800000">@"<span style="color: #800000">--documents--%@<span style="color: #800000">"<span style="color: #000000">, documents);<span style="color: #008000">//<span style="color: #008000">Add/number to make it a complete path Database_path =<span style="color: #000000">[documents stringbyappendingpathcomponent:dbname]; NSLog (<span style="color: #800000">@"<span style="color: #800000">--database_path---%@<span style="color: #800000">"<span style="color: #000000">, database_path); db =<span style="color: #000000">[fmdatabase databasewithpath:database_path];}<span style="color: #008000">//<span style="color: #008000">Initialize the user interface-(<span style="color: #0000ff">void<span style="color: #000000">) addview{<span style="color: #008000">//<span style="color: #008000">New Database UIButton *createbtn=<span style="color: #000000">[UIButton buttonwithtype:uibuttontyperoundedrect]; Createbtn.frame=cgrectmake (<span style="color: #800080">60,<span style="color: #800080">60,<span style="color: #800080">200,<span style="color: #800080">50<span style="color: #000000">); [CREATEBTN addtarget:self action: @selector (doclickcreatebutton) forcontrolevents:uicontroleventtouchupinside]; [CREATEBTN settitle:<span style="color: #800000">@"<span style="color: #800000">CreateTable<span style="color: #800000">"<span style="color: #000000">forstate:uicontrolstatenormal]; [self.view addsubview:createbtn];<span style="color: #008000">//<span style="color: #008000">Insert Database UIButton *insterbtn=<span style="color: #000000">[UIButton buttonwithtype:uibuttontyperoundedrect]; Insterbtn.frame=cgrectmake (<span style="color: #800080">60,<span style="color: #800080">130,<span style="color: #800080">200,<span style="color: #800080">50<span style="color: #000000">); [INSTERBTN addtarget:self action: @selector (doclickinsertbutton) forcontrolevents:uicontroleventtouchupinside]; [INSTERBTN settitle:<span style="color: #800000">@"<span style="color: #800000">Insert<span style="color: #800000">"<span style="color: #000000">forstate:uicontrolstatenormal]; [self.view addsubview:insterbtn];<span style="color: #008000">//<span style="color: #008000">Update Database UIButton *updatebtn=<span style="color: #000000">[UIButton buttonwithtype:uibuttontyperoundedrect]; Updatebtn.frame=cgrectmake (<span style="color: #800080">60,<span style="color: #800080">200,<span style="color: #800080">200,<span style="color: #800080">50<span style="color: #000000">); [UPDATEBTN addtarget:self action: @selector (doclickupdatebutton) forcontrolevents:uicontroleventtouchupinside]; [UPDATEBTN settitle:<span style="color: #800000">@"<span style="color: #800000">Update<span style="color: #800000">"<span style="color: #000000">forstate:uicontrolstatenormal]; [self.view addsubview:updatebtn];<span style="color: #008000">//<span style="color: #008000">Delete Database UIButton *deletebtn=<span style="color: #000000">[UIButton buttonwithtype:uibuttontyperoundedrect]; Deletebtn.frame=cgrectmake (<span style="color: #800080">60,<span style="color: #800080">270,<span style="color: #800080">200,<span style="color: #800080">50<span style="color: #000000">); [DELETEBTN addtarget:self action: @selector (doclickdeletebutton) forcontrolevents:uicontroleventtouchupinside]; [DELETEBTN settitle:<span style="color: #800000">@"<span style="color: #800000">Delete<span style="color: #800000">"<span style="color: #000000">forstate:uicontrolstatenormal]; [self.view addsubview:deletebtn];<span style="color: #008000">//<span style="color: #008000">View Database UIButton *selectbtn=<span style="color: #000000">[UIButton buttonwithtype:uibuttontyperoundedrect]; Selectbtn.frame=cgrectmake (<span style="color: #800080">60,<span style="color: #800080">340,<span style="color: #800080">200,<span style="color: #800080">50<span style="color: #000000">); [SELECTBTN addtarget:self action: @selector (doclickselectbutton) forcontrolevents:uicontroleventtouchupinside]; [SELECTBTN settitle:<span style="color: #800000">@"<span style="color: #800000">Select<span style="color: #800000">"<span style="color: #000000">forstate:uicontrolstatenormal]; [self.view addsubview:selectbtn];<span style="color: #008000">//<span style="color: #008000">Multithreading UIButton *multithreadbtn=<span style="color: #000000">[UIButton buttonwithtype:uibuttontyperoundedrect]; Multithreadbtn.frame=cgrectmake (<span style="color: #800080">60,<span style="color: #800080">410,<span style="color: #800080">200,<span style="color: #800080">50<span style="color: #000000">); [MULTITHREADBTN addtarget:self action: @selector (doclickmultithreadbutton) forcontrolevents: uicontroleventtouchupinside]; [MULTITHREADBTN settitle:<span style="color: #800000">@"<span style="color: #800000">Multithread<span style="color: #800000">"<span style="color: #000000">forstate:uicontrolstatenormal]; [self.view addsubview:multithreadbtn]; }<span style="color: #0000ff">#pragma-mark-doclickaction<span style="color: #008000">//<span style="color: #008000">New database-(<span style="color: #0000ff">void<span style="color: #000000">) doclickcreatebutton{<span style="color: #008000">//<span style="color: #008000">SQL statements<span style="color: #0000ff">If<span style="color: #000000">([db Open]) {nsstring *sqlcreatetable = [nsstring stringwithformat:<span style="color: #800000">@"<span style="color: #800000">CREATE TABLE IF not EXISTS '%@ ' ('%@ ' integer PRIMARY KEY autoincrement, '%@ ' text, '%@ ' integer, '%@ ' Text)<span style="color: #800000">"<span style="color: #000000">, tablename,id,name,age,address]; BOOL res =<span style="color: #000000">[db executeupdate:sqlcreatetable];<span style="color: #0000ff">If (!<span style="color: #000000">Res) {NSLog (<span style="color: #800000">@"<span style="color: #800000">Error when creating DB table<span style="color: #800000">"<span style="color: #000000">); }<span style="color: #0000ff">Else<span style="color: #000000">{NSLog (<span style="color: #800000">@"<span style="color: #800000">Success to creating DB table<span style="color: #800000">"<span style="color: #000000">); } [db close]; }}<span style="color: #008000">//<span style="color: #008000">Insert database-(<span style="color: #0000ff">void<span style="color: #000000">) doclickinsertbutton{<span style="color: #0000ff">If<span style="color: #000000">([db Open]) {nsstring *insertsql1=<span style="color: #000000">[nsstring stringwithformat:<span style="color: #800000">@"<span style="color: #800000">INSERT into '%@ ' ('%@ ', '%@ ', '%@ ') VALUES ('%@ ', '%@ ', '%@ ')<span style="color: #800000">"<span style="color: #000000">, TABLENAME, NAME, age, ADDRESS,<span style="color: #800000">@"<span style="color: #800000">Tom<span style="color: #800000">",<span style="color: #800000">@"<span style="color: #800000">13<span style="color: #800000">",<span style="color: #800000">@"<span style="color: #800000">Jinan<span style="color: #800000">"<span style="color: #000000">]; BOOL res =<span style="color: #000000">[db executeupdate:insertsql1];<span style="color: #0000ff">If (!<span style="color: #000000">Res) {NSLog (<span style="color: #800000">@"<span style="color: #800000">Error when insert DB table<span style="color: #800000">"<span style="color: #000000">); }<span style="color: #0000ff">Else<span style="color: #000000">{NSLog (<span style="color: #800000">@"<span style="color: #800000">Success to insert DB table<span style="color: #800000">"<span style="color: #000000">); } NSString *INSERTSQL2 =<span style="color: #000000">[nsstring stringwithformat:<span style="color: #800000">@"<span style="color: #800000">INSERT into '%@ ' ('%@ ', '%@ ', '%@ ') VALUES ('%@ ', '%@ ', '%@ ')<span style="color: #800000">"<span style="color: #000000">, TABLENAME, NAME, age, ADDRESS,<span style="color: #800000">@"<span style="color: #800000">John doe<span style="color: #800000">",<span style="color: #800000">@"<span style="color: #800000">12<span style="color: #800000">",<span style="color: #800000">@"<span style="color: #800000">Jinan<span style="color: #800000">"]; BOOL res2 = [db executeupdate:insertsql2]; If (!res2) {NSLog (@ "error when insert db table");} Else{NSLog (@ "success to insert DB table");} [db close]; }}//modify database-(void) doclickupdatebutton{if ([db open]) {nsstring *updatesql = [nsstring stringwithformat: @ ' UPDATE '%@ ' SET '%@ ' = '%@ ' WHERE '%@ ' = '%@ ' ", TABLENAME, age, @" at 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 database-(void) doclickdeletebutton{if ([db open]) {nsstring *deletesql = [nsstring stringwithformat: @ ' Delete from%@ where%@ = '%@ ' ", TABLENAME, NAME, @" Zhang San "]; 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%@", TA blename]; Fmresultset * rs = [db executequery:sql]; While ([rs next]) {int Id = [rs intforcolumn:id]; NSString * name = [rs stringforcolumn:name]; NSString * age = [rs stringforcolumn:age]; NSString * address = [rs stringforcolumn:address]; NSLog (@ "id =%d, name =%@, age =%@ address =%@", id, name, age, address); } [db close]; }}//multithreaded Operations 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 <; ++i) {[queue indatabase:^ (fmdatabase *db2) {nsstring *insertsql1= [NSSt Ring 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 <; ++i) {[queue indatabase:^ (fmdatabase *db2) {nsstring *insertsql2= [NSSt Ring 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), or else {NSLog (@ "succ to Inster data:%@", name);}]; } }); }-(void) didreceivememorywarning {[super Didreceivememorywarning];//Dispose of any of the resources that can be recreated.} @end</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy" style="font-size: 18px"><span class="cnblogs_code_copy" style="font-size: 18px"></span></span><p><p>"code note" Ios-fmdbdemo</p></p></span>
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.