Adventures in Machine Learning

Migrating a Web2py-Based CRM App to MySQL: A Step-by-Step Guide

Converting a Web2py-based CRM App from SQLite to MySQL

In today’s fast-paced digital world, businesses need to harness the power of data to compete effectively. Customer Relationship Management (CRM) systems are essential tools for businesses to collect and analyze data about their customers.

However, as businesses continue to grow, their data management needs also increase, and they may have to switch to a more robust database management system to handle this growth. This is where converting a CRM app from SQLite to MySQL comes into play.

Data Cleanse

The first step in converting a Web2py-based CRM app from SQLite to MySQL is to carry out a data cleanse. This process involves removing any duplicates, inconsistencies, or inaccuracies in the data.

You can download a database dump of your SQLite data and open it with a text editor to carry out this process.

MySQL Setup

Once you have cleaned up your data, the next step is to set up MySQL. You need to create a new database, a username, and a password.

You can use this database to store your CRM data, and the username and password will authenticate your access to the database. Before you start the migration process, it’s important to test your MySQL database in a local environment to ensure that it’s working correctly.

Adding Data

With MySQL successfully set up, the next step is to add the data from your SQLite database to MySQL. To do this, you need to execute the output.sql command from the SQLite database.

This will generate a file that contains all the SQL commands required to create your MySQL database. Once you have generated the output.sql file, open the file in a text editor and delete the first two lines.

After that, you can execute the commands in the file using the MySQL admin tool or the MySQL command line interface.


Converting a Web2py-based CRM app from SQLite to MySQL may sound like a daunting task, but it’s an essential step in managing your business’s data effectively. With a little bit of planning and preparation, you can successfully migrate your data to MySQL and continue to extract value from your CRM system.

Remember to carry out a data cleanse, set up MySQL, and add your data in a structured and organized manner. By following these simple steps, you can ensure a smooth transition to MySQL and keep your CRM system running smoothly.

Current Database Structure

The current database structure of a Web2py-based CRM app consists of various tables and fields that store important information about customers, sales transactions, and other data points. These tables and fields are created using the Database Abstraction Layer (DAL) that is an integral part of the web2py framework.

One of the essential tables in this database is the “users” table, which contains customer and user information. This table has several fields such as id, start_date, and end_date, which store essential details about customers and users.

Apart from the “users” table, there are other tables such as the “campaigns” table that store information about marketing campaigns, the “leads” table that contains information about potential customers, and the “sales” table that stores information about sales transactions. In this Web2py-based CRM app, there are approximately 30,000 rows of data that need to be migrated from SQLite to MySQL.

Cleaning the Data for MySQL Conversion

Before migrating the data from SQLite to MySQL, it’s essential to clean the data to ensure that the data is accurate and that there are no duplicates or inconsistencies. Here are some of the steps involved in cleaning data for MySQL conversion:

Editing the output_before.sql file

The first step in cleaning the data involves editing the output_before.sql file that contains the SQL commands required to create the MySQL database.

In the output_before.sql file, there are some PRAGMA statements that are specific to SQLite and need to be removed. Next, add the “BEGIN TRANSACTION” statement at the beginning of the file, and the “COMMIT” statement at the end of the file.

These statements ensure that the data is inserted into the MySQL database as a single transaction, which is useful for preventing data inconsistencies and duplication. Another important step is to delete the “sqlite_sequence” table.

This table is used by SQLite to keep track of the auto-incrementing primary keys in a table. Since this is not relevant to MySQL, there is no need to include this table in the MySQL database.

Then, the DELETE statement should be used to remove the existing data in the tables of the MySQL database. This ensures that there are no duplicates or inconsistencies.

Afterward, modify the INSERT INTO statements by removing the double quotes and adding backticks around the table and field names. This is because MySQL uses backticks instead of double quotes for escaping table and field names.

Finally, save the output_before.sql file, and it is now ready to be executed in MySQL.


Converting a Web2py-based CRM app from SQLite to MySQL involves a series of steps that require attention to detail. By understanding the database structure and cleaning the data beforehand, businesses can migrate to MySQL successfully.

Editing the output_before.sql file by removing SQLite-specific statements and ensuring data consistency is the key step in this process. Overall, following these steps for cleaning the data ensures that businesses can reap the benefits of a robust database management system.

Configuring Web2py to Use MySQL

Web2py is a popular framework for developing web-based apps that includes a Database Abstraction Layer (DAL) that allows developers to interact with multiple database types without having to write database-specific code. In this context, setting up Web2py to use MySQL is a crucial step in migrating the CRM app from SQLite to MySQL.

Editing the File

The first step in configuring Web2py to use MySQL is editing the file, which is part of the Web2py application. This file defines the database connection parameters, including the database type, host, port, database name, username, and password.

In this case, we need to set the database type to “mysql” and provide the necessary details about the MySQL database. To do this, we open the file and add the following lines of code:


db = DAL(‘mysql://username:password@host/dbname’, migrate_enabled=True, lazy_tables=True)


Here, replace “username,” “password,” “host,” and “dbname” with the specific details of your MySQL database.

Next, check if the connection between the application and the database has been established correctly by going to the “web2py admin” interface.

Initializing MySQL database

Before migrating the data to MySQL, we need to backup the original data on SQLite and delete it from the database. Upon ensuring that the data is properly backed up; we can then access the database administration button and click “initialize” to get the application ready for migration.

Checking Data Accessibility in the New MySQL Database

Once the new MySQL database is initialized, the next step is to check if the data is still accessible. Access data accessibility through the web2py admin interface by selecting the database that was created.

To do this, navigate to the “Database” section of the Web2py admin interface. Click on “Application: [application name]” which will expand to show the accessible databases.

We can also verify if the data in MySQL is accessible from the application by running a simple query. In the controller file of the Web2py application, we can add the following code snippet to fetch the first five rows of data from a table named “users” and print it to the console:


def index():

rows = db(db.users).select(limitby=(0, 5))


return dict()


After running this code, we should be able to see the output of the first five rows of data in the “users” table in the console.


In conclusion, migrating a Web2py-based CRM app from SQLite to MySQL is a crucial step in managing data more efficiently. By understanding how to configure Web2py to use MySQL, the business can take advantage of the numerous benefits that come with a robust database management system.

Therefore, it is essential to follow the proper steps such as editing the file and checking the data’s accessibility in the new MySQL database to ensure a smooth migration. Ultimately, this process leads to an enhanced CRM system that can better support the business’s data-related needs, facilitating decision-making and driving growth.

In conclusion, converting a Web2py-based CRM app from SQLite to MySQL is an important process that businesses need to consider to manage data efficiently. The process of migrating to MySQL requires cleaning the data, editing the output_before.sql file, configuring Web2py to use MySQL, and verifying the accessibility of the new data.

By following these steps correctly, businesses can benefit from a robust database management system and make informed decisions based on accurate data. This shows the importance of data management, which is the foundation of any business.

Therefore, businesses must prioritize this process and take the critical steps to ensure data accuracy before it is used for decision-making in the success of the business.

Popular Posts