-(Void) copyDBFile
{
NSFileManager * manager = [NSFileManager defamanager manager];
// If the file exists, it will not be copied
If ([manager fileExistsAtPath: [self databasePath]) {
Return;
}
NSString * atPath = [[NSBundle mainBundle] pathForResource: kDatabaseName ofType: nil];
// Copy the database file to the sandbox path
[Manager copyItemAtPath: atPath toPath: [self databasePath] error: nil];
}
// Obtain the path of the database file
-(NSString *) databasePath
{
// NSLog (@ "% @", [[NSBundle mainBundle] pathForResource: kDatabaseName ofType: nil]);
Return [NSHomeDirectory () stringByAppendingFormat: @ "/Library/% @", kDatabaseName];
// Return [[NSBundle mainBundle] pathForResource: kDatabaseName ofType: nil];
}
// Add a data entry
-(BOOL) addUser :( User *) user
{
// Database object
Sqlite3 * mysqlite = nil;
// 1. Open the database
// Filename: Path of the database file (string of C)
// Sqlite3: database object to be executed
Int openResult = sqlite3_open ([[self databasePath] UTF8String], & mysqlite );
// If the open function is successfully executed, 0 and SQLITE_ OK are returned.
If (openResult! = SQLITE_ OK ){
NSLog (@ "database opening failed ");
Return NO;
}
// 2. Prepare SQL statements
// You can regard the content stored in sqlite3_stmt * as an SQL statement.
Sqlite3_stmt * stmt = nil;
// Construct an SQL statement
NSString * SQL = [NSString stringWithFormat: @ "insert into UserTable (username, password, phone, age) values (\" % @ \ ", \" % @\", \ "% @ \", % ld) ", user. username, user. password, user. phone, user. age];
// Sqlite3: database object to be executed
// ZSql: SQL statement
// Stmt: Object saved by the statement
Sqlite3_prepare (mysqlite, [SQL UTF8String],-1, & stmt, NULL );
// 3. Execute the SQL statement
Int stepResult = sqlite3_step (stmt );
If (stepResult = SQLITE_ERROR | stepResult = SQLITE_MISUSE ){
NSLog (@ "execution failed ");
}
// 4. SQL statement completion
Sqlite3_finalize (stmt );
// 5. Close the database
Sqlite3_close (mysqlite );
Return YES;
}
4. CoreData
-(Void) openDataBase
{
// 1. Load the data model file xcdatamodeld
NSURL * url = [[NSBundle mainBundle] URLForResource: @ "DataModel" withExtension: @ "momd"];
// NSManagedObjectModel is used to load the data model file.
NSManagedObjectModel * dataModel = [[NSManagedObjectModel alloc] initWithContentsOfURL: url];
// 2. Open the database file corresponding to the model file
// Create a coordinator object and use the Coordinator to manage database files
NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: dataModel];
// Specify the location of the database file in the sandbox
NSString * storePath = [NSHomeDirectory () stringByAppendingString: @ "/Documents/DataStore. sqlite"];
NSURL * storeURL = [NSURL fileURLWithPath: storePath];
// Open a data file PersistentStore --> database file
/*
1. If the file does not exist, create a new database file.
2. If the file exists, open the file directly.
*/
NSError * error = nil;
[Psc addPersistentStoreWithType: NSSQLiteStoreType // The generated database file format is SQLite
Configuration: nil
URL: storeURL // path for storing database files
Options: nil
Error: & error];
If (error ){
NSLog (@ "database file opening failed ");
} Else
{
NSLog (@ "database opened successfully ");
}
// 3. Operate the database
Context = [[NSManagedObjectContext alloc] init];
// Specifies which coordinator the context uses to Operate Database Files
Context. persistentStoreCoordinator = psc;
}