+ (Sqlite3 *) opendb
{
if (db = = nil) {
Get the path to the document file
Parameter 1: folder name parameter 2: Find domain Parameter 3: Use absolute path
NSString *docpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) firstObject];
(2) path to the database file
NSString *dbpath = [DocPath stringbyappendingpathcomponent:file_name];
Class for managing files in iOS, responsible for copying files, deleting files, moving files (and file-related actions)
Nsfilemanager *FM = [Nsfilemanager Defaultmanager];
Determine if there are sqlite files in document
if (![ FM Fileexistsatpath:dbpath])
{
The path of the SQLite file in *.app;
NSString *bundlepath = [[NSBundle mainbundle] pathforresource:@ "DataBase" oftype:@ "SQLite"];
Nserror *error = nil;
Copy Bundlepath to DBPath
BOOL result = [FM copyitematpath:bundlepath topath:dbpath error:&error];
if (!result) {
NSLog (@ "%@", error);//If error printing error message occurs;
}
}
Open Database
Parameter 1: File path; Parameter 2: Receiving a pointer to a database
Sqlite3_open ([DbPath utf8string], &db);
}
return DB;
}
+ (void) closedb
{
Sqlite3_close (DB);
db = nil;
}
Code implementation Open and close the database