Mysql command
How to connect python to mysql
"""
Created on Fri Feb 4 08:34:15 2022
@author: shubham Prajapati
Date:.....
"""
import mysql.connector as sql
mycon = sql.connect(host ="localhost",user = 'root',passwd = "kv3jam")
print("Connected sucessfully..")
mycur = mycon.cursor()
mycur.execute("use school")
#--------------------------------------------------------------------
'''Qry = "create table dept\
( deptno int(3) primary key,\
dname varchar(15) NOT NULL,\
city varchar(15))"
mycur.execute(Qry)
#-------------------------------------------------------------------------
Qry = "desc dept"
mycur.execute(Qry)
for r in mycur:
print(r)
'''
'''
#Qry = "Insert into dept values(103,'HR', 'Ahmedabad')"
Qry= "insert into dept values({},'{}','{}')".format(105,'Sales',"mumbai")
#Qry="insert into dept values(%s,'%s','%s')" %(104,'marketing','Baroda')
mycur.execute(Qry)
mycon.commit()
Qry="delete from dept where deptno = 110"
#Qry="delete from dept where deptno=%s" %(103,)
Qry = "update dept\
set city ='%s'\
where deptno =%s" %('Baroda',105)
Qry = "delete from hospital where name like '%Vinita%' "
mycur.execute(Qry)
mycon.commit()
for r in mycur:
print(r)
print("Total Records recieved:", mycur.rowcount)
'''
Qry = "select * from hospital"
mycur.execute(Qry)
data=mycur.fetchmany(50)
for r in data:
print(r)
print("Total Records recieved:", mycur.rowcount)
Another program
"""
Created on Tue Feb 1 08:46:22 2022
@author: kv
"""
import mysql.connector as sql
mycon = sql.connect(host ="localhost",user = 'root',passwd = "kv3jam", database= "school")
print("Connected sucessfully..")
mycur = mycon.cursor()
str = "show databases"
mycur.execute(str)
for db in mycur:
print(db)
mycur.execute("use school")
print("List of table in school database:")
mycur.execute("show tables")
for tbl in mycur:
print(tbl)
mycur.execute("Select * from hospital where department = 'ortho'")
for rec in mycur:
print(rec)
New updated program
import mysql.connector as sql
mycon=sql.connect(host="localhost",user="root",passwd="Krish5102004.com",database="Exam")
mycursor=mycon.cursor()
mycursor.execute("select * from CLUB")
s=mycursor.fetchall()
for i in s:
print(i)
No comments:
If you have any doubt so you can comment me then i will 100% help you ,Through comment Chat