- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
If you check contents of employee table, it will show new record added in it.
- 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?
- Java application to insert null value into a MySQL database?
- Database INSERT Operation in Python
- How to get the id after INSERT into MySQL database using Python?
- Insert current date to the database in MySQL?
- How do we insert/store a file into MySQL database using JDBC?
- How to insert a record into a table in a database using JDBC API?
- How to insert Timestamp value in a database using JDBC program?
- How to form a MySQL Conditional Insert?
- How do I get the id after INSERT into MySQL database in Python?
- How to do a batch insert in MySQL?
- How to insert an image in to MySQL database using Java program?