When you want to connect to the MySQL database in Python code, you can use both the mysql-connector-python MySQL driver and PyMySQL. The mysql-connector-python MySQL driver is a MySQL server built-in driver and the PyMySQL is a third-party library.
All the two MySQL python libraries provide classes for you to get MySQL database connection, execute insert, delete, update, select SQL command. They also support transaction management. This article will show you an example of how to use mysql-connector-python and PyMySQL to operate on MySQL database table.
1. How To Use mysql-connector-python To Operate MySQL Database.
1.1 Install mysql-connector-python Library.
- The mysql-connector-python library can be installed when you install the MySQL database.
- So you can run the command
pip show mysql-connector-python
to check whether it has been installed or not.$ pip show mysql-connector-python WARNING: Package(s) not found: mysql-connector-python
- If the mysql-connector-python library is not installed, you can run the command
pip install mysql-connector-python
to install it.$ pip install mysql-connector-python Collecting mysql-connector-python Downloading mysql_connector_python-8.0.25-py2.py3-none-any.whl (319 kB) |████████████████████████████████| 319 kB 219 kB/s Collecting protobuf>=3.0.0 Downloading protobuf-3.17.0-cp37-cp37m-macosx_10_9_x86_64.whl (959 kB) |████████████████████████████████| 959 kB 727 kB/s Requirement already satisfied: six>=1.9 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from protobuf>=3.0.0->mysql-connector-python) (1.15.0) Installing collected packages: protobuf, mysql-connector-python Successfully installed mysql-connector-python-8.0.25 protobuf-3.17.0
- Now when you run the command
pip show mysql-connector-python
again, you can get it’s install information.$ pip show mysql-connector-python Name: mysql-connector-python Version: 8.0.25 Summary: MySQL driver written in Python Home-page: http://dev.mysql.com/doc/connector-python/en/index.html Author: Oracle and/or its affiliates Author-email: UNKNOWN License: GNU GPLv2 (with FOSS License Exception) Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages Requires: protobuf Required-by:
1.2 Use mysql-connector-python Connect To MySQL Database Steps.
- Import the mysql.connector class.
import mysql.connector
- Invoke the mysql.connector.connect() method to connect to MySQL database server.
# get mysql connection object. def open_mysql_connection(user='jerry', password='jerry', host='127.0.0.1', port='3306', database='dev2qa_example', use_unicode=True): conn = mysql.connector.connect(user=user, password=password, host=host, port=port, database=database, use_unicode=use_unicode) return conn
1.3 Execute DDL Statement To Create A Table.
- Get MySQL database cursor.
cursor = conn.cursor()
- Execute DDL statement. If you want to execute multiple DDL statements at one time, you need to separate each DDL statement with a semicolon, and you should add the multi=True
parameter to the cursor object’sexecute()
method, otherwise it will throw the error mysql.connector.errors.InterfaceError: Use multi=True when executing multiple statements.def execute_ddl(conn): cursor = conn.cursor() # The below DDL will first drop the table if exist, then create the table. ddl_sql= ''' DROP TABLE user_account; CREATE TABLE `dev2qa_example`.`user_account` ( `id` INT NOT NULL AUTO_INCREMENT, `user_name` VARCHAR(45) NULL, `password` VARCHAR(45) NULL, `email` VARCHAR(45) NULL, PRIMARY KEY (`id`)); ''' # Because the above DDL contains 2 statements ( drop, create), so need to add the multi=True parameter to avoid error mysql.connector.errors.InterfaceError: Use multi=True when executing multiple statements. cursor.execute(ddl_sql, multi=True) cursor.close() conn.close() print(ddl_sql + ' execute successfully.')
1.4 Execute DML ( Insert, Update, Delete ) Statement.
- Get MySQL database connection cursor object.
- Run the insert, update, delete SQL statement with the cursor object’s
execute()
method.def execute_dml(conn): cursor = conn.cursor() dml_insert = ''' INSERT INTO `dev2qa_example`.`user_account` VALUES (null, %s, %s, %s); ''' cursor.execute(dml_insert, ('tom', 'tom12345678', 'tom@gmail.com')) conn.commit() cursor.close() conn.close()
- If you want to insert multiple rows into the MySQL table, you can execute the cursor object’s
executemany()
method.def execute_dml_insert_many(conn): cursor = conn.cursor() dml_insert = ''' INSERT INTO `dev2qa_example`.`user_account` VALUES (null, %s, %s, %s); ''' row_tuple = (('richard', 'richard', 'richard@gmail.com'), ('trump', 'trump', 'trump@gmail.com'),('doctor', 'doctor', 'doctor@gmail.com')) cursor.executemany(dml_insert, row_tuple) conn.commit() cursor.close() conn.close()
1.5 Execute DML Select SQL Statement.
- Run the cursor object’s
execute()
method to run select SQL statement.def execute_dml_select(conn): cursor = conn.cursor() dml_select = 'SELECT * FROM dev2qa_example.user_account;' cursor.execute(dml_select) # print the row header. for col in cursor.description: print(col[0], end='\t') print('\n-----------------------------') # print each row data. for row in cursor: print(row) print(row[1] + '--->' + row[2]) cursor.close() conn.close()
2. How To Use PtMySQL To Operate MySQL Database.
2.1 Install PyMySQL.
- Before you can use PyMySQL, you should install it first. You can use pip to install like below.
$ pip3 install PyMySQL
- You can run
pip3 show
command to verify PyMySQL installation.$ pip3 show PyMySQL
2.2 Use PyMySQL To Connect And Operate MySQL Database.
Below are the steps that you use PyMySQL library in Python code.
- Import the PyMSQL connect, cursors class.
from pymysql import connect, cursors
- Call connect method to get MySQL database connection object.
conn = connect(host=host, user=user, password=password, db=db, charset=charset, cursorclass=cursorclass)
- Get database cursor object by connection object’s cursor() function.
conn.cursor() as cursor:
- Execute the SQL statement use the cursor object.
cursor.execute(sql)
- If the sql statement is a select statement, then call cursor object’s fetchall() or fetchone() method to get the sql execution result. Please note after invoke fetchall(), the cursor will move to the last row of the result, so if you call fetchall() method again, there will have no row returned.
row = cursor.fetchall() or row = cursor.fetchone()
- If the sql statement is a insert, update, delete statement, then call connection object’s commit() function to commit the changes to MySQL database to take effect.
conn.commit()
- Do not forget to close the MySQL database connection object at the end to release database resources.
if conn is not None: conn.close() conn = None
2.3 PyMySQL Operate MySQL Database Example.
- In this example, the MySQL database name is dev2qa_example, the table name is user_account. The table has four columns that are id, user_name, password, and email. The id column is auto-incremented.
- The python source code contains methods to insert, update, delete and select user account data like below.
from pymysql import connect, cursors global_host='127.0.0.1' global_user='jerry' global_password='jerry' global_db='dev2qa_example' global_charset='utf8' global_cursorclass=cursors.DictCursor # get mysql connection object. def open_mysql_connection(host='127.0.0.1', user='jerry', password='jerry', db='dev2qa_example', charset='utf8', cursorclass=cursors.DictCursor): conn = connect(host=host, user=user, password=password, db=db, charset=charset, cursorclass=cursorclass) return conn # close mysql connection. def close_mysql_connection(conn): if conn is not None: conn.close() conn = None # execute insert, update or delete sql command. def execute_insert_update_delete_sql(sql, host, user, password, db, charset, cursorclass): execute_row_count = 0 conn = None print('sql = ' + sql) try: if string_is_not_empty(sql): conn = open_mysql_connection(host, user, password, db, charset, cursorclass) with conn.cursor() as cursor: execute_row_count = cursor.execute(sql) except Exception as ex: print(ex) finally: if conn is not None: conn.commit() close_mysql_connection(conn) return execute_row_count # execute select sql command. def execute_select_sql(sql, host, user, password, db, charset, cursorclass): ret = list() conn = None print('sql = ' + sql) try: if string_is_not_empty(sql): conn = open_mysql_connection(host, user, password, db, charset, cursorclass) with conn.cursor() as cursor: row_count = cursor.execute(sql) print('select return row count = ' + str(row_count)) row = cursor.fetchall() print(row) ret.append(row) except Exception as ex: print(ex) finally: if conn is not None: close_mysql_connection(conn) return ret # insert user account to database table dev2qa_example.user_account. def insert_user_account(user_name, password, email): print('**********Begin insert_user_account.**********') if string_is_not_empty(user_name) and string_is_not_empty(password) and string_is_not_empty(email): # first check whether user account exist or not. sql = "select count(id) as count from dev2qa_example.user_account where lower(user_name) = '"+user_name.lower().strip()+"'" row_list = execute_select_sql(sql, global_host, global_user, global_password, global_db, global_charset, global_cursorclass) account_exist = False for row in row_list: for column in row: exist_count_number = column.get('count') if exist_count_number > 0: account_exist = True if not account_exist: print('User ' + user_name + ' do not exist in database. Insert the user account to database table.') sql = "insert into user_account(user_name, password, email) values('"+user_name+"','"+password+"','"+email+"')" insert_row_count = execute_insert_update_delete_sql(sql, global_host, global_user, global_password, global_db, global_charset, global_cursorclass) print('Insert ' + str(insert_row_count) + ' row data successfully.') else: print('User account exist, can not insert.') else: print('user_name, password, email can not be empty.') print('**********End insert_user_account.**********') # update user account data. def update_user_account(user_name, password, email): print('**********Begin update_user_account.**********') if string_is_not_empty(user_name): sql = "update dev2qa_example.user_account set password = '" + password + "', email = '" + email + "' where lower(user_name) = '" + user_name.lower() + "'" update_row_count = execute_insert_update_delete_sql(sql, global_host, global_user, global_password, global_db, global_charset, global_cursorclass) if update_row_count == 0: print('User account do not exist, insert it now.') insert_user_account(user_name, password, email) else: print('Update ' + str(update_row_count) + ' row data successfully.') else: print('user_name can not be empty.') print('**********End update_user_account.**********') # delete user account data from database table. def delete_user_account(user_name): print('**********Begin delete_user_account.**********') if string_is_not_empty(user_name): sql = "delete from dev2qa_example.user_account where lower(user_name) = '"+user_name.lower()+"'" delete_row_count = execute_insert_update_delete_sql(sql, global_host, global_user, global_password, global_db, global_charset, global_cursorclass) print('Delete ' + str(delete_row_count) + ' row data successfully.') print('**********End delete_user_account.**********') # execute select sql command to get user account data by user_name. def get_user_account_by_user_name(user_name): print('**********Begin get_user_account_by_user_name.**********') sql = "select * from dev2qa_example.user_account where lower(user_name) = '"+user_name.lower().strip()+"'" row_list = execute_select_sql(sql, global_host, global_user, global_password, global_db, global_charset, global_cursorclass) print('**********End get_user_account_by_user_name.**********') # check whether the string is empty or not. def string_is_not_empty(str): if str is None: return False elif len(str.strip()) == 0: return False else: return True if __name__ == '__main__': # first delete user account jerry. delete_user_account('jerry') # then insert a user account jerry into database. insert_user_account('jerry', 'jerry', 'jerry@dev2qa.com') # show user account data to verify the insert action. get_user_account_by_user_name('jerry') # update usr account information. update_user_account('jerry', 'jerry888', 'jerry@gmail.com') # check the updated user account info again. get_user_account_by_user_name('jerry')
- Below is the above code execution result.
**********Begin delete_user_account.********** sql = delete from dev2qa_example.user_account where lower(user_name) = 'jerry' Delete 1 row data successfully. **********End delete_user_account.********** **********Begin insert_user_account.********** sql = select count(id) as count from dev2qa_example.user_account where lower(user_name) = 'jerry' select return row count = 1 [{'count': 0}] User jerry do not exist in database. Insert the user account to database table. sql = insert into user_account(user_name, password, email) values('jerry','jerry','jerry@dev2qa.com') Insert 1 row data successfully. **********End insert_user_account.********** **********Begin get_user_account_by_user_name.********** sql = select * from dev2qa_example.user_account where lower(user_name) = 'jerry' select return row count = 1 [{'id': 42, 'user_name': 'jerry', 'password': 'jerry', 'email': 'jerry@dev2qa.com'}] **********End get_user_account_by_user_name.********** **********Begin update_user_account.********** sql = update dev2qa_example.user_account set password = 'jerry888', email = 'jerry@gmail.com' where lower(user_name) = 'jerry' Update 1 row data successfully. **********End update_user_account.********** **********Begin get_user_account_by_user_name.********** sql = select * from dev2qa_example.user_account where lower(user_name) = 'jerry' select return row count = 1 [{'id': 42, 'user_name': 'jerry', 'password': 'jerry888', 'email': 'jerry@gmail.com'}] **********End get_user_account_by_user_name.**********
References