-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpython_sql_operations.py
68 lines (57 loc) · 1.93 KB
/
python_sql_operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# ####### CREATE A MYSQL DATABASE ##########
# import MySQLdb
# mysqldb = MySQLdb.connect(host="localhost",user="root",password="mysql123!@#")
# mycursor = mysqldb.cursor()
# mycursor.execute("create database dbpython")
#### CONNECT TO DATABASE #######
import MySQLdb
mysqldb = MySQLdb.connect(host="127.0.0.1",user="root",password="mysql123!@#",database="dbpython")
mycursor = mysqldb.cursor()
####Create a table into dbpython database if not exists ########
table_name = 'student'
table_exists= False
try:
mycursor.execute(f"SHOW TABLES LIKE '{table_name}' ;")
result=mycursor.fetchall()
if(len(result)>0):
table_exists= True
print(f'{table_name} TABLE already exists..!')
if(not table_exists):
mycursor.execute(f"create table {table_name}(roll INT,name VARCHAR(255), marks INT)")
print(f'{table_name} TABLE created sucessfully..!')
except Exception as ee:
print('Error:Unable to fetch data.' + str(ee))
### INSERT RECORD TO MYSQL #######
try:
mycursor.execute("insert into student values(1,'Sarfaraj',80),(2,'Kumar',89),(3,'Sohan',90)")
mysqldb.commit()
print('Record inserted successfully...')
except:
mysqldb.rollback()
### DISPLAY RECORDS ########
try:
mycursor.execute("select * from student")
result=mycursor.fetchall()
for i in result:
roll=i[0]
name=i[1]
marks=i[2]
print(roll,name,marks)
except:
print('Error:Unable to fetch data.')
#### UPDATE RECORD ###
import mysql.connector
try:
mycursor.execute("UPDATE student SET name='Ramu', marks=100 WHERE roll=1")
mysqldb.commit()
print('Record updated successfully...')
except:
mysqldb.rollback()
#### DELETE RECORD ####
import mysql.connector
try:
mycursor.execute("DELETE FROM student WHERE roll=3")
mysqldb.commit()
print('Record deteted successfully...')
except:
mysqldb.rollback()