Store data in MySQL using Python : sample code
Store data in MYSQL in Python
Many times in your python application you may need to store results or some data into relational database and MySQL is one of the most widely used relational database.
In this post we are presenting you with a sample script to insert records in MySQL table. In below example we will be using mysql connector python for demonstration.
Enjoy Python.
Many times in your python application you may need to store results or some data into relational database and MySQL is one of the most widely used relational database.
In this post we are presenting you with a sample script to insert records in MySQL table. In below example we will be using mysql connector python for demonstration.
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
try:
connection = mysql.connector.connect(host='localhost',
database='test',
user='root',
password='root')
print("connection is open")
query = """INSERT INTO Person (`Name`, `Age`, `DOB`, `Address`)
VALUES ('ABC', 134,'2001-08-14', 'Rajasthan, India') """
cursor = connection.cursor()
cursor.execute(query)
connection.commit()
print(cursor.rowcount, "Record inserted successfully")
cursor.close()
except mysql.connector.Error as error:
print("Error while inserting record {}".format(error))
finally:
if (connection.is_connected()):
connection.close()
Enjoy Python.
Comments
Post a Comment