You are on page 1of 4

#to insert data into any table from any database

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)

Q2=input("you want to alter table?(yes/NO)")#if you want to alter any


table#you can also just click enter button
if Q2=="yes" or Q2=="YES":
T3=input("table name")#example- empl,orders etc
N4=int(input("enter no.of columns"))#if you
for ii in range(N4):
M=input("enter condition")
if M=="modify" or M=="MODIFY":
CA=input("enter column & data type")
C=input("enter constraint")#not null
try:
mc.execute("alter table "+T3+" modify
"+CA+" "+C)
mydatabase.commit()
except mysql.connector.Error as error:
print("query cannot be executed, there's
some error:",error)
mc.execute("desc "+T3)
for x in mc:
print(x)

elif M=="add" or M=="ADD" or M=="DROP" or M=="drop":


W9=input("want to add any
constraints?(yes/no)")#you can also just click enter button
if W9=='yes' or W9=='YES':
C9=input("column name")
C10=input("enter
constraint")#null,pri,fK,UK
try:
mc.execute("alter table "+T3+"
"+M+" "+C10+"("+C9+")")
mydatabase.commit()
except mysql.connector.Error as error:
print("query cannot be executed,
there's some error:",error)
mc.execute("desc "+T3)
for x in mc:
print(x)
else:
CA=input("enter column name:")#don't
mention datatype
try:
mc.execute("alter table "+T3+"
"+M+" "+CA)
mydatabase.commit()
except mysql.connector.Error as error:
print("query cannot be executed,
there's some error:",error)
mc.execute("desc "+T3)
for x in mc:
print(x)
elif M=="rename" or M=="RENAME":
C=input("enter column name:")#don't mention
datatype
A=input("enter new column name:")
try:
mc.execute("alter table "+T3+" rename
column "+C+" to "+A)
mydatabase.commit()
except mysql.connector.Error as error:
print("query cannot be executed, there's
some error:",error)
mc.execute("desc "+T3)
for x in mc:
print(x)
#the below functions are DML commands which don't use <executemany> function

q=input("you want insert data?(yes/NO)")#if you want to insert#you can also


just click enter button
if q=="yes" or q=="YES":
T=input("table name")#here u can mention the table in which you
want to insert data
n1=int(input("enter no.of records"))#no. of rows you want to insert
print("enter no.of columns in ",T);num=int(input(":-"))
num1=num-1;B="%s ";D=B*num1
A=D.replace(" ",",")+B
for i in range(n1):
values=[]
print("new row")
for i in range(num):
try:
X=eval(input("value_E"))
values.append(X)
except:
print("there's ERROR please check input")
try:
mc.execute("insert into "+T+"
values("+A+")",tuple(values))
print("rows affected ",mc.rowcount)
mydatabase.commit()
except mysql.connector.Error as error:
print("query cannot be executed, there's some
error:",error)
mc.execute("select * from "+T)
for x in mc:
print(x)
Q=input("you want to update data?(yes/NO)")#if you want to update#you can
also just click enter button
if Q=="yes" or Q=="YES":#update data function below
t=input("table name")
N=int(input("enter no.of records"))
for i in range(N):
Q1=input("enter columns and and new values")
C=input("enter column & existing values for where clause ")
try:
mc.execute("update "+t+" set "+Q1+" where "+C)
print("rows affected ",mc.rowcount)
mydatabase.commit()
except mysql.connector.Error as error:
print("query cannot be executed, there's some
error:",error)
mc.execute("select * from "+t)
for x in mc:
print(x)

q1=input("you want to delete data?(yes/NO)")#if you want to delete#you can


also just click enter button
if q1=="yes" or q1=="YES":#delete data function below
T1=input("table name")
N1=int(input("enter no.of records"))
for i in range(N1):
try:
Q=input("enter any column(unique)with value to
delete the date(row)")
mc.execute("delete from "+T1+" where "+Q)
print("rows affected ",mc.rowcount)
mydatabase.commit()
except mysql.connector.Error as error:
print("query cannot be executed, there's some
error:",error)
mc.execute("select * from "+T1)
for x in mc:
print(x)
ZZ=input("want to run the program again(yes/NO)")#you can also just click
enter button
if ZZ=="yes" or ZZ=="YES":
begin()
else:
mc.close()
mydatabase.close()
if mydatabase.is_connected():
print("still connected")
else:
print("the connection between mysql/python is closed")

#program on python and mysql by j ashok chandra class 12th commerce, year 2020

You might also like