
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to insert a Python tuple in a MySQL database?
Assuming that MySQL database named as test is present on server and a table named employee is also created. The table has five fields fname, lname, age, gender, and salary.
A tuple object containing data of a record is defined as
t1=('Mac', 'Mohan', 20, 'M', 2000)
To establish interface between MySQL and Python 3, you need to install PyMySQL module. Then you can set up the connection using following statements
import PyMySQL # Open database connection db = PyMySQL.connect("localhost","root","","test" ) # prepare a cursor object using cursor() method cursor = db.cursor()
Next step is to set up the insert query using data in the tuple
sql="insert into employee values(%s,%s,%d,%s,%d)" %t1
Now this query is executed using execute() method of cursor object
cursor.execute(sql)
If you check contents of employee table, it will show new record added in it.
- Related Articles
- How to insert a Python tuple in a PostgreSql database?
- How to insert data into a MySQL database with Java?
- How to insert DECIMAL into MySQL database?
- How to insert a Python object in MySQL?
- How to get the id after INSERT into MySQL database using Python?
- How to insert raw data in MySQL database in Laravel?
- Java application to insert null value into a MySQL database?
- Insert current date to the database in MySQL?
- How do I get the id after INSERT into MySQL database in Python?
- Database INSERT Operation in Python
- How to insert an image in to MySQL database using Java program?
- How do we insert/store a file into MySQL database using JDBC?
- How to insert Timestamp value in a database using JDBC program?
- More elegant way to insert empty java.sql.Date in MySQL Database?
- How to do a batch insert in MySQL?

Advertisements