SQLITE underlying operating SQLITE learning (a)

1. Create a database

The SQLite sqlite3 command is used to create a new SQLite database. It does not require any special privileges to create a data.
Note : Use sqlite3 command can also open an existing sqlite database. Normally, the name of the database in the RDBMS (relational database management system) should be unique.

  • Creating a database of grammar

     $sqlite3 DatabaseName.db
    
  • Creating a database specific operation is as follows:
    Here Insert Picture Description
    The above testDB.db command creates a file in the current directory. The document will be used as the database engine SQLite. If you have noticed sqlite3 command after successfully create a database file, it will provide a sqlite> prompt. And there is the figure shows the database is created, you can use .database command to see if it is in the list of databases.
    Supplementary:
    .dump command ------ can use SQLite .dump point command at a command prompt to export a complete database in a text file, as follows:

     $sqlite3 demo.db .dump > demo.sql
    

The above command will convert the contents of entire demo.db database to SQLite statement, and dump it into an ASCII text file demo.sql in. You can restore a simple way from demo.sql generated as follows:

	$sqlite3 testDB.db < testDB.sql

2, additional databases

Assume such a case, when there are multiple database is available at the same time, you want to use any of them. The SQLite ATTACH DATABASE statement is used to select a particular database, the use of this command, all statements SQLite performed in the additional database.
Additional database basic syntax:

ATTACH DATABASE 'DatabaseName' As 'Alias-Name';

If the database has not been created, the above command will create a database, if the database already exists, put the database file names and logical database 'Alias-Name' to bind together.
Specific operation is as follows:
Here Insert Picture Description
Note:
the database names are reserved for main and temp database master database tables and temporary objects and other temporary data. These two databases connected to each database names may be used, and should not be used for additional, or will give a warning message, as shown below:

sqlite>  ATTACH DATABASE 'demo.db' as 'TEMP';
Error: database TEMP is already in use
sqlite>  ATTACH DATABASE 'demo.db' as 'main';
Error: database main is already in use;

3, separate database

The DETACH DTABASE SQLite statement is used to connect to the database name and freed from a separate database, it is connected prior to use additional ATTACH statement. If the same database file has been attached on multiple aliases, DETACH command to disconnect only the given name of the connection, while the rest are still valid. You can not separate main or temp database.
** Note: ** If the database is in memory or a temporary database, the database will be destroyed, and the content will be lost.

Detach the database basic syntax:

DETACH DATABASE 'Alias-Name';

Here, 'Alias-Name' and you use the same statement when ATTACH additional database alias used before.
Specific operation is as follows:

  • We created a database, and attach it to a 'test' and 'current', use the command .database

     sqlite>.databases
     seq  name             file
     ---  ---------------  ----------------------
     0    main           /home/sqlite/demo.db
     2    test             /home/sqlite/demo.db
     3    current       /home/sqlite/demo.db
    

Now the 'current' separated from test.db out as follows:

sqlite> DETACH DATABASE 'current';

Now, if you examine the currently attached database found demo.db remain connected to the 'test' and the 'main'.

sqlite>.databases
seq  name             file
---  ---------------  ----------------------
0    main             /home/sqlite/demo.db
2    test             /home/sqlite/demo.db

4, create a table

SQLite CREATE TABLE statement for any given database to create a new table. Create basic table relates to data type named table, defining columns and each column .
sqlite create basic syntax tables:

CREATE TABLE database_name.table_name(
   column1 datatype  PRIMARY KEY(one or more columns),
   column2 datatype,
   column3 datatype,
   .....
   columnN datatype,
);

CREATE TABLE tells the database system creates a new table of keywords. After the CREATE TABLE statement followed the table a unique name or logo. You can also choose to specify database_name with the table_name.

It creates a company, department table, id as the primary key, not null constraint representation when creating a record in a table the field can not be null: the specific operation is as follows:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

5, delete the table

SQLite is a DROP TABLE statement is used to delete a table definition and all data, indexes, triggers, constraints, and permission specifications of the table.
Note : Pay special attention when you use this command, because once a table is deleted, all the information in the table will be lost forever.

The basic syntax sqlite delete tables:

DROP TABLE database_name.table_name;

To do sqlite delete tables:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/cainiaoxiakexing/article/details/91875290