after completing all the downloads that we have seen on the last part page(check)
for writing the code, you can use any text editor.
First, we will see how to write a sample MySQL database connection in python. After that, we will move to a responsive connection on a web server.
|
import mysql.connector mydb=mysql.connector.connect( host="localhost"
, user= "root" , password= "", ) print(mydb) mycurusor=mydb.cursor() |
the initial state we have to give create a new database connection through python to mysql.
after everything has done alright it will provide the object hexadecimal values. here, the cursor denotes invoking the MySQL queries for execution.
database creation :
|
mycurusor.execute("CREATE DATABASE pyfile") print("database
created....") mycurusor.execute("SHOW DATABASES") |
The above comments are used for creating the database on our MySQL. after that it will show the all the database's names available on MySQL.
|
mydb=mysql.connector.connect( host="localhost"
, user= "root" , password= "", database=”pyfile” ) |
Creation of Table:
|
mycurusor.execute("CREATE TABLE sudha (tennum varchar(20),name
varchar(20),clg varchar(20))") print("tabel created....") |
this code is used to create a table on our databases.
insertion the data's:
|
insert=(
"INSERT INTO sudha (tennum,name,clg)" "VALUES (%s,%s,%s)") print("enter the tentative number") n=input("enter the
tentative number") n1= input ("enter the
name of student") n2= Input ("enter the
clg name") data=(n,n1,n2) mycurusor.execute(insert,data); mydb.commit() print("inserted...") |
the above code is explained about how to write the query for inserting the data.
after running the above code your given values going to store in the database.
|
delete=(
"DELETE sudha from clg=%s") n3=input("enter the clg name") data2=(n3) mycurusor.execute(delete,data2) mydb.commit() print( "data deletedd successfully...") |
the above process for deleting the data from the database.
The final code for this blog is:
|
import mysql.connector mydb=mysql.connector.connect( host="localhost"
, user= "root" , password= "", database=”pyfile” ) mycurusor.execute("CREATE DATABASE pyfile") print("database created....") mycurusor.execute("SHOW DATABASES") mycurusor.execute("CREATE TABLE sudha (tennum
varchar(20),name varchar(20),clg varchar(20))") print("tabel created....") insert=( "INSERT INTO sudha (tennum,name,clg)"
"VALUES (%s,%s,%s)") print("enter the tentative number") n=input("enter the tentative number") n1= input ("enter the name of student") n2= Input ("enter the clg name") data=(n,n1,n2) mycurusor.execute(insert,data); mydb.commit() print("inserted...") delete=(
"DELETE sudha from clg=%s") n3=input("enter the clg name") data2=(n3) mycurusor.execute(delete,data2) mydb.commit() print( "data deletedd successfully...") |
finally your database of Mysql looks like this.


No comments:
Post a Comment