Skip to content

Commit 559949e

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#485 from mauryaankitsh/patch-4
By this we can insert data in mysql
2 parents 80540dc + 71b77f3 commit 559949e

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import mysql.connector
2+
from mysql.connector import Error
3+
from mysql.connector import errorcode
4+
5+
try:
6+
connection = mysql.connector.connect(host='localhost',
7+
database='electronics',
8+
user='root',
9+
password='pynative@#29')
10+
mySql_insert_query = """INSERT INTO Laptop (Id, Name, Price, Purchase_date)
11+
VALUES
12+
(10, 'Lenovo ThinkPad P71', 6459, '2019-08-14') """
13+
14+
cursor = connection.cursor()
15+
cursor.execute(mySql_insert_query)
16+
connection.commit()
17+
print(cursor.rowcount, "Record inserted successfully into Laptop table")
18+
cursor.close()
19+
20+
except mysql.connector.Error as error:
21+
print("Failed to insert record into Laptop table {}".format(error))
22+
23+
finally:
24+
if (connection.is_connected()):
25+
connection.close()
26+
print("MySQL connection is closed")
27+

0 commit comments

Comments
 (0)