0% found this document useful (0 votes)
12 views4 pages

Research of It Formation

Uploaded by

arun bala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Research of It Formation

Uploaded by

arun bala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

http://localhost/phpmyadmin

1.]Insert function
2.]select function
3.]update function
4.]delete function

CRUD - create,read,update,delete.

Install:
pip install mysql.connector

-----------------Insert function------------------------------

import mysql.connector
mysql=mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="python_mysql"
)

mycursor=mysql.cursor()
sql="insert into table1 values('Mani',25,'cbe','[email protected]')"

mycursor.execute(sql)
mysql.commit()

print("data saved")

------------------select function---------------------------

import mysql.connector
mysql=mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="python_mysql"
)

mycursor=mysql.cursor()
sql="select * from table1"

mycursor.execute(sql)
myresult=mycursor.fetchall() # fetchone - all

for row in myresult:


print(row)

-----------------------------------
import mysql.connector
mysql=mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="sakthi"
)

mycursor=mysql.cursor()
sql="select * from table1"
mycursor.execute(sql)
myresult=mycursor.fetchmany(size=5) # fetchone() - fetchall() -fetchmany(size=2)

for row in myresult:


print(row)

-------------------update function--------------------------

import mysql.connector
mysql=mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="python_mysql"
)

mycursor=mysql.cursor()
sql="update table1 set name='sakthi' where name='mani'"
try:
mycursor.execute(sql)
mysql.commit()
print('data updates')

except:
print("Unable to update")

mysql.close()

-----------------------delete function---------------------

import mysql.connector
mysql=mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="python_mysql"
)

mycursor=mysql.cursor()
sql="delete from table1 where Name ='sakthi'"
try:
mycursor.execute(sql)
mysql.commit()
print('data deleted')

except:
print("Unable to delete")

mysql.close()

-----------------------databace-------------------------------

import mysql.connector
mysql = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="sam",
)

mycursor = mysql.cursor()
UserName=(input("enter the name\n"))
Age=int(input("enter the age\n"))
"""DATA INSERTING"""
sql = "insert into table3 (name,age) values(%s,%s)"
data=(UserName,Age)
mycursor.execute(sql,data)
mysql.commit()
print("data saved")

"""DATA SELECT"""
sql="select * from table3"
mycursor.execute(sql)
myresult=mycursor.fetchmany(size=2)
for row in myresult:
print(row)

mysql.close()

--------------------------------------

from tabulate import tabulate


import mysql.connector

con = mysql.connector.connect(host="localhost", user="root", password="",


database="CRUD")

def insert(name, age, city,id):


res = con.cursor()
sql = "insert into users (id,name,age,city) values (%s,%s,%s,%s)"
user = (name, age, city,id)
res.execute(sql, user)
con.commit()
print("Data Insert Success")

def update(name, age, city,id):


res = con.cursor()
sql = "update users set name=%s,age=%s,city=%s where id=%s"
user = (name, age, city,id)
res.execute(sql, user)
con.commit()
print("Data Update Success")
def select():
res = con.cursor()
sql = "SELECT ID,NAME,AGE,CITY from users"
res.execute(sql)
# result=res.fetchone()
# result=res.fetchmany(2)
result = res.fetchall()
print(tabulate(result, headers=["ID", "NAME", "AGE", "CITY"]))

def delete(id):
res = con.cursor()
sql = "delete from users where id=%s"
user = (id,)
res.execute(sql, user)
con.commit()
print("Data Delete Success")

while True:
print("1.Insert Data")
print("2.Update Data")
print("3.Select Data")
print("4.Delete Data")
print("5.Exit")
choice = int(input("Enter Your Choice : "))
if choice == 1:
id = input("Enter the ID : ")
name = input("Enter Name : ")
age = input("Enter Age : ")
city = input("Enter City : ")
insert(name, age, city,id)
elif choice == 2:
id = input("Enter The Id : ")
name = input("Enter Name : ")
age = input("Enter Age : ")
city = input("Enter City : ")
update(name, age, city,id)
elif choice == 3:
select()
elif choice == 4:
id = input("Enter The Id to Delete : ")
delete(id)
elif choice == 5:
quit()
else:
print("Invalid Selection . Please Try Again !")

#SELECT COUNT(age) FROM table1


#SELECT LIMIT(0,5) FROM table1,SELECT * LIMIT 0,5 FROM table1
#SELECT DISTINCT AGE FROM table1
-----------------------------------------

You might also like