SQLite Tutorial (V): Database and transaction _sqlite

Source: Internet
Author: User
Tags sqlite table name sqlite tutorial

One, attach database:

The ATTACH database statement adds another database file to the current connection, and if the file name is ": Memory:" We can treat it as a memory database and the memory databases cannot be persisted to disk files. If you are manipulating a table in a attached database, you need to add a database name, such as Dbname.table_name, to the table name. The last thing to note is that if a transaction contains multiple attached database operations, then the transaction is still atomic. See the following example:

Copy Code code as follows:

Sqlite> CREATE TABLE testtable (First_col integer);
Sqlite> INSERT into TestTable VALUES (1);
sqlite>. Backup ' d:/mydb.db '-backs up the primary database in the current connection to the specified file.
Sqlite>. Exit
--Re-logon to the SQLite command line tool:
Sqlite> CREATE TABLE testtable (First_col integer);
Sqlite> INSERT into TestTable VALUES (2);
Sqlite> INSERT into TestTable VALUES (1);
sqlite> ATTACH DATABASE ' d:/mydb.db ' as mydb;
Sqlite>. Header on--Query Results output field names as headers.
sqlite>. Mode column--displays each column separately.
Sqlite> SELECT t1.first_col from testtable T1, mydb.testtable t2 WHERE t.first_col = T2.first_col;
First_col
----------
1

Second, the Detach database:

Uninstalls the specified database in the current connection, noting that the main and temp databases cannot be uninstalled. See the following example:
Copy Code code as follows:

-This example hosts the result of the example above, that is, the MyDB database has been attach to the current connection.
sqlite> DETACH DATABASE mydb;
Sqlite> SELECT t1.first_col from testtable T1, mydb.testtable t2 WHERE t.first_col = T2.first_col;
Error:no such table:mydb.testtable

third, the business:

In SQLite, if a specified transaction is not displayed for the current SQL command (except Select), then SQLite automatically adds an implicit transaction to the operation to ensure the atomicity and consistency of the operation. Of course, SQLite also supports the display of transactions, whose syntax is essentially the same as for most relational databases. See the following example:

Copy Code code as follows:

Sqlite> BEGIN TRANSACTION;
Sqlite> INSERT into TestTable VALUES (1);
Sqlite> INSERT into TestTable VALUES (2);
Sqlite> COMMIT TRANSACTION; --Displays the transaction being committed and the data in the datasheet changes.
Sqlite> SELECT COUNT (*) from testtable;
COUNT (*)
----------
2
Sqlite> BEGIN TRANSACTION;
Sqlite> INSERT into TestTable VALUES (1);
Sqlite> ROLLBACK TRANSACTION; --shows that the transaction is rolled back and the data in the datasheet has not changed.
Sqlite> SELECT COUNT (*) from testtable;
COUNT (*)
----------
2

Related Article

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.