Code to open and close the database, code to close the database
+ (Sqlite3 *) openDB
{
If (db = nil ){
// Obtain the path of the Document file
// Parameter 1: folder name parameter 2: Search domain parameter 3: whether to use absolute path
NSString * docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
// (2) path of the database file
NSString * dbPath = [docPath stringByAppendingPathComponent: FILE_NAME];
// Ios file management class, responsible for copying, deleting, and moving files (file-related operations)
NSFileManager * fm = [NSFileManager defamanager manager];
// Determine whether the sqlite file exists in the document
If (! [Fm fileExistsAtPath: dbPath])
{
// * Path of the sqlite file in the 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); // print the error message if an error occurs;
}
}
// Open the database
// Parameter 1: file path; parameter 2: receive database pointer
Sqlite3_open ([dbPath UTF8String], & db );
}
Return db;
}
+ (Void) closeDB
{
Sqlite3_close (db );
Db = nil;
}
How to enable and disable databases in JAVA
Since it has been encapsulated, it can be encapsulated in the constructor of the parent class, so that the sub-classes can inherit from the constructor so that they can connect to the database when creating this object.
Of course, you can also use the string ioc to add the database connection method and the method to close the database connection before and after the method you want to execute.
Seniors: Ask C # How to write code to implement transaction processing. For example, updating the order table is a process of opening and closing the database link and updating the Order details.
Private static void ExecuteSqlTransaction (string connectionString)
{
Using (SqlConnection connection = new SqlConnection (connectionString ))
{
Connection. Open ();
SqlCommand command = connection. CreateCommand ();
SqlTransaction transaction;
// Start a local transaction.
Transaction = connection. BeginTransaction ("SampleTransaction ");
// Must assign both transaction object and connection
// To Command object for a pending local transaction
Command. Connection = connection;
Command. Transaction = transaction;
Try
{
Command. CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'description ')";
Command. ExecuteNonQuery ();
Command. CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'description ')";
Command. ExecuteNonQuery ();
// Attempt to commit the transaction.
Transaction. Commit ();
Console. WriteLine ("Both records are written to database .");
}
Catch (Exception ex)
{
Console. WriteLine ("Commit Exception Type: {0}", ex. GetType ());
Console. WriteLine ("Message: {0}", ex. Message );
// Attempt to roll back the transaction.
Try
{
... The remaining full text>