CREATING TABLE AND INSERTING DATA INTO THE TABLE WITH PYTHON!!

avatar

Well this is a simple program where you can create a table in your existing database and insert data into that table from python jupyter using SQL query. This one is for creating table in one of my existing database leoumesh.

import mysql.connector

db=mysql.connector.connect(host="localhost", user="root", passwd="", database="leoumesh")

mycursor=db.cursor()

myquery="CREATE TABLE MANAGER (Manager_ID INT NOT NULL,Manager_Name VARCHAR(30),Department_Name VARCHAR(30))"


mycursor.execute(myquery)

print("Table successfully created.")

db.close()


Output after successfully executing the code.


Screenshot_1.png

And here you can see the table manager has been created.


Screenshot_2.png

Now the next thing I want to do is insert data into this table. For this, I run the following code:

import mysql.connector

db=mysql.connector.connect(host="localhost", user="root", passwd="", database="leoumesh")

mycursor=db.cursor()

myquery='INSERT INTO manager(Manager_ID, Manager_Name, Department_Name) VALUES(1,"Jerry", "Purchase")'


mycursor.execute(myquery)

print("Data successfully inserted.")

db.close()



This is the output after I executed this program.


Screenshot_3.png

And you can see one of the data have been inserted into the MySQL database by executing the above code.


Screenshot_4.png



0
0
0.000
1 comments
avatar
UpvoteBank
Your upvote bank
__2.jpgThis post have been upvoted by the @UpvoteBank service. Want to know more and receive "free" upvotes click here
0
0
0.000