Convert SQLite3 file to SQL file
1. Save the .sqlite3 file and export it as a .sql file
Before exporting, make sure you have sqlite
this library installed
downloadsqlite
pip install sqlite
The next step is simple
Use the following command to export to SQL file. :
sqlite3 database.sqlite .dump > sqlite_dump.sql
database.sqlite
It is the name of the SQLite database, sqlite_dump.sql
which is the exported SQL format data file.
2. Convert the content of the SQL file (that is, organize the SQL file)
Delete the following two lines at the beginning, MySQL does not support these two lines of code
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
Delete the related statements of the last few sqlite system tables
These tables are used by sqlite to save some parameters of our database, and have nothing to do with the database itself
ANALYZE sqlite_master;//以下到最后的所有语句
Modify MySQL incompatible syntax
The SQL file exported by SQLite cannot be directly used in MySQL, and some different syntax needs to be converted. The main differences are as follows:
- SQLite has some statements that MySQL does not support, such as
BEGIN TRANSACTION
,COMMIT
,sqlite_sequence
. - Some statements of SQLite table creation are not compatible with MySQL. For example
varchar not null
, this kind of execution in MySQL will report an error. - SQLite creates a data table with double quotes:
""
, but MySQL uses upper quotes: ````. - In the SQLite index creation statement, the table name and field are used in double quotes, which are similar to the above one and need to be replaced with upper quotes.
- SQLite uses
t
andf
to express boolean values, MySQL uses1
and0
. - SQLite self-increment attribute keyword is
AUTOINCREMENT
, MySQL isAUTO_INCREMENT
.
The conversion step is the most time-consuming, but when I was making this note, there were fewer SQL statements after conversion, and I did not encounter the above-mentioned incompatible syntax. There is no experimental practice here, so it is impossible to determine whether the above is every SQLite file. That's all.
3. Import MySQL
After the conversion in the second step, you can directly use mysql
the command to import SQL data.
Just run the dump directly on Navicat.
First create an empty database with the same name as the sql file, then open the database
and right-click to run the sql file.