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)