Code to open and close the database, code to close the database

Source: Internet
Author: User

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>

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.