SQLite (Databases and things)

Source: Internet
Author: User

First, attach database:

The ATTACH database statement adds another file to the current connection, and if the file name is ": Memory:", we can treat it as a memory database and the memory database cannot be persisted to the disk file. If you manipulate a table in the attached database, you need to add the database name, such as Dbname.table_name, before the table name. Finally, it is necessary to note that if a transaction contains multiple attached database operations, the transaction is still atomic. See the example below:
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-login to 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 ' asMyDB
Sqlite>. Header on--The query results output the field name as a caption.
Sqlite>. Mode column--Displays each column separately.
Sqlite> SELECT t1.first_col from TestTable T1,mydb.testtableT2 WHERE t.first_col = T2.first_col;
First_col
----------
1

Second, detach database:

Uninstall the specified database in the current connection, and note that the main and temp databases cannot be uninstalled. See the example below:
--The example hosts the results of the above example, where the MyDB database has been attach to the current connection.
Sqlite>DETACH DATABASEMyDB
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 no specified transaction is displayed for the current SQL command (select except), SQLite automatically adds an implicit transaction for 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 most relational databases. See the example below:
Sqlite>BEGIN TRANSACTION;
Sqlite> INSERT into TestTable VALUES (1);
Sqlite> INSERT into TestTable VALUES (2);
Sqlite>COMMIT TRANSACTION;--shows that the transaction is committed and the data in the data table has changed.
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 data table has not changed.
Sqlite> SELECT COUNT (*) from testtable;
COUNT (*)
----------
2

SQLite (Databases and things)

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.