Professional Documents
Culture Documents
Just 197 Lines
Just 197 Lines
def begin():
import mysql.connector
mydatabase=mysql.connector.connect(host="localhost",user="root",password="1234")
mc=mydatabase.cursor()#below function for displaying data only
n=int(input("enter no."))#no .of querries u want to execute
for i in range(n):
q=input("enter query")#enter queries for displaying data
try:
mc.execute(q)
for x in mc:
print(x)
except mysql.connector.Error as error:
print("query cannot be executed, there's some
error:",error)
#it's not necessary to give input "no",you can also just click enter button and
ignore i.e skip some functions
q2=input("you want to create database or table?(yes/No)")#you can also just
click enter button
if q2=="yes" or q2=="YES":#say yes
n2=int(input("no.of tables/databases"))#if you enter 2 then you can
create two tables/databases
T2=input("table/database name")#example--persons
syntax1=input("<table/database>")#enter table or database
for i in range(n2):
if syntax1=="database":
try:
mc.execute("create database "+T2)
mydatabase.commit()
except mysql.connector.Error as error:
print("query cannot be executed, there's
some error:",error)
mc.execute("show databases")
for x in mc:
print(x)
elif syntax1=="table":
print("ex-code int, name char(3) & don't use '()'")
F=input("enter column and their datatypes")#simply
specify columns
try:
mc.execute("create "+syntax1+"
"+T2+"("+F+")")
mydatabase.commit()
except mysql.connector.Error as error:
print("query cannot be executed, there's
some error:",error)
try:
mc.execute("desc "+T2)
for x in mc:
print(x)
except mysql.connector.Error as error:
print("query cannot be execute, there's
some error:",error)
Q1=input("you want to(drop/truncate)database or table?(yes/NO)")#user
choice#you can also just click enter button
if Q1=="yes" or Q1=="YES":
N2=int(input("no.of tables/databases"))#1 tables or more
syntax=input("pick <table/database>")#enter table or database
for i in range(N2):
t1=input("table/database name")# example- empl,orders etc
M8=input("do you want truncate instead of
drop(yes/no)")#you can also just click enter button
if M8=='yes' or M8=='YES':
try:
mc.execute("truncate table "+t1)
mydatabase.commit()
except mysql.connector.Error as error:
print("query cannot be executed, there's
some error:",error)
mc.execute("show tables")
for U in mc:
print(U)
else:
try:
mc.execute("drop "+syntax+" "+ t1)
mydatabase.commit()
except mysql.connector.Error as error:
print("query cannot be executed, there's
some error:",error)
mc.execute("show "+syntax+"s")
for x in mc:
print(x)
#program on python and mysql by j ashok chandra class 12th commerce, year 2020