Exporting MySQL Database to SQLite: Command Line and Scripting Approaches
There are two main approaches to achieve this export and import:
Using SQL commands:
Export:
Using intermediary formats:
Important Considerations:
- Data type compatibility: Ensure data types between MySQL and SQLite are compatible during conversion.
- Advanced features: Complex MySQL features like stored procedures or triggers might not have direct equivalents in SQLite and require alternative solutions.
This example exports a table named products
with a primary key from a MySQL database named mystore
to a new SQLite database named store.db
.
Export (assuming MySQL credentials are username: 'root' and password: 'mypassword')
mysqldump --compatible=ansi --compact mystore products -u root -pmypassword | grep -v "^\/\*" | sqlite3 store.db
Explanation:
mysqldump
: The command to dump the MySQL database.--compatible=ansi
: Ensures the output is compatible with ANSI SQL standards for better import into SQLite.--compact
: Removes extra information from the output.mystore
: The name of the MySQL database.products
: The specific table to export (optional, exports entire database if omitted).-u root
: Username for MySQL access.-pmypassword
: Password for MySQL access (be cautious using password in scripts).grep -v "^\/\*"
: Filters out comments starting with "/" from the output.sqlite3 store.db
: Establishes a connection to the new SQLite databasestore.db
.|
: Pipes the output frommysqldump
to thesqlite3
command for execution.
Using Python and libraries (more advanced):
This example demonstrates a basic approach using Python libraries mysql.connector
and sqlite3
to achieve the same functionality.
import mysql.connector
import sqlite3
# Define connection details
mysql_host = "localhost"
mysql_user = "root"
mysql_password = "mypassword"
mysql_database = "mystore"
sqlite_file = "store.db"
# Connect to MySQL database
mysql_conn = mysql.connector.connect(host=mysql_host, user=mysql_user, password=mysql_password, database=mysql_database)
mysql_cursor = mysql_conn.cursor()
# Select data from the table
mysql_cursor.execute("SELECT * FROM products")
data = mysql_cursor.fetchall()
# Connect to SQLite database
sqlite_conn = sqlite3.connect(sqlite_file)
sqlite_cursor = sqlite_conn.cursor()
# Create table in SQLite (adjust according to your table schema)
sqlite_cursor.execute("""CREATE TABLE products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
price REAL
)""")
# Insert data into SQLite table
for row in data:
sqlite_cursor.execute("INSERT INTO products (name, price) VALUES (?, ?)", row[1:]) # Exclude ID for auto-increment
# Commit changes and close connections
sqlite_conn.commit()
mysql_conn.close()
sqlite_conn.close()
print("Export completed!")
Remember:
- Replace placeholders like usernames, passwords, database names, and table structure with your specific details.
- This is a simplified example. Error handling and more robust connection management would be necessary in a production environment.
Third-party migration tools:
Scripting with other languages:
While the previous examples used Python, languages like Perl or PHP can also achieve data migration. Libraries like DBI
(Perl) or dedicated database connectors can be used to interact with both MySQL and SQLite, allowing for custom script development tailored to your specific needs.
Choosing the right method depends on several factors:
- Technical expertise: Command-line tools might require more technical knowledge, while GUI tools offer a user-friendly interface.
- Complexity of data: For simple database structures, scripting could be sufficient. For complex migrations, dedicated tools might offer better control and automation.
- Frequency of migration: If you need to perform the migration regularly, scripting or a dedicated tool might save time in the long run.
mysql database sqlite