1. Put the database file localdata.db into the project and build the bundle (in Build phases)
2. Create a new class for the local SQLite query. LOCALDB.M (. h did not say, to ensure that each of the. m inside the function to be called externally is good interface in. h)
3.localdb.m:createdb function
Because the bundle files in the project cannot be used directly (only the files in document can be used directly), the first time you use the SQLite file, you copy it to the document folder.
-(void) createdb{Nsarray*paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); //This method can be used to display file information in the document folderNSString *documents = [Paths Objectatindex:0]; Database_path= [Documents Stringbyappendingpathcomponent:dbname];//gets the address of the database file that does not exist and createsNSLog (@"The database address is:%@", Database_path); //using the Fileexistsatpath method of the object of the Nsfilemanager class to detect the presence of the Dbfilepath, a bool value is returned based on the well-stitched path .//1. Create a Nsfilemanager object Nsfilemanager the method that contains the file attributesNsfilemanager *FM =[Nsfilemanager Defaultmanager]; //2. By Nsfilemanager Object FM to determine if the file exists, there is a return yes does not exist return noBOOL isexist =[FM Fileexistsatpath:database_path]; //NSLog (@ "Isexist =%d", isexist); //If there is no isexist = no, copy the database from the project to documents if(!isexist) { //Copy Database//Get the path to the database in the project, because we've added the database file to the project, so we're going to get the path from the project .NSString *backupdbpath =[[NSBundle Mainbundle] Pathforresource:@"Localdata"OfType:@"DB"]; BOOL CP=[FM Copyitematpath:backupdbpath Topath:database_path Error:nil]; NSLog (@"found bundle file:%@, copied to document", Backupdbpath); } if(Sqlite3_open ([Database_path utf8string], &db)! =SQLITE_OK) {sqlite3_close (db); NSLog (@"Database open Failed"); } sqlite3_close (db);}
4. Simple query statement (I created a dbversion table, then the field is Dbversion, and the value is 1.0 for future updates)
This is one of the simplest uses of SQLite's query statements.
-(NSString *) selectdbversion{NSString*version; if(Sqlite3_open ([Self->database_path utf8string], &db) = =SQLITE_OK) {NSString*sqlquery =@"SELECT dbversion from Dbversion"; Sqlite3_stmt*statement; if(SQLITE3_PREPARE_V2 (DB, [SQLQuery utf8string],-1, &statement, nil) = =SQLITE_OK) { while(Sqlite3_step (statement) = =Sqlite_row) { Char*row0 = (Char*) Sqlite3_column_text (statement,0); Version=[[NSString alloc]initwithutf8string:row0]; } }Else{NSLog (@"Failed from Sqlite3_prepare_v2. Error is:%s", sqlite3_errmsg (db)); } } returnversion;}
5. External Call mode:
Instantiating the LocalDB object is possible.
LocalDB *newdb=[LocalDB alloc]; [newdb createdb]; *version=[newdb selectdbversion]; NSLog (@ "%@", version);
[How to use IOS SQLite]