-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11_PythonMySQL.py
More file actions
75 lines (62 loc) · 2.01 KB
/
day11_PythonMySQL.py
File metadata and controls
75 lines (62 loc) · 2.01 KB
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
69
70
71
72
73
74
75
'''
Python MySQL Connector
It is a python driver that helps to integrate Python and MySQL.
'''
# Connecting to MySQL Server
#importing required libraries
import mysql.connector
dataBase= mysql.connector.connect(
host='localhost',
user='root',
passwd='',
database='College'
)
#preparing a cursor object
cursorobj=dataBase.cursor()
#creatting a database
'''cursor.execute("CREATE DATABASE College")'''
#creating a table
'''StudentRec="""CREATE TABLE STUDENT(NAME VARCHAR(20) NOT NULL, SECTION VARCHAR(10), ID INT NOT NULL)"""
cursorobj.execute(StudentRec)'''
#inserting single row into table
'''sql = "INSERT INTO STUDENT (NAME, SECTION, ID) VALUES (%s, %s, %s)" # In mysql.connector %s is the placeholder used for all data types including strings, integers, floats, etc.
val = ("Ngima", "CG10", 10)
cursorobj.execute(sql, val)
dataBase.commit()
dataBase.close()'''
#inserting multiple rows into table
'''sql="INSERT INTO STUDENT( NAME,SECTION,ID) VALUES (%s,%s,%s)"
val=[("Rabi","CG10",20),("Rosi","CG11",30),("Numa","CG12",40)]
cursorobj.executemany(sql,val)
dataBase.commit() '''
#Select data from MySQL table using Python
'''query="SELECT * FROM STUDENT"
cursorobj.execute(query)
result=cursorobj.fetchall()
for x in result:
print(x)'''
#Select all data from MySQL table using Python
'''query="SELECT * FROM STUDENT where ID >=20"
cursorobj.execute(query)
result=cursorobj.fetchall()
print(result)'''
#updating the data from table
'''query="UPDATE STUDENT SET ID= 35 WHERE NAME='Numa'"
cursorobj.execute(query)
dataBase.commit() #to save changes
result=cursorobj.fetchall()
print(result)'''
#deleting the data from table
'''query="DELETE FROM STUDENT WHERE NAME='Numa'"
cursorobj.execute(query)
dataBase.commit() #to save changes
print(cursorobj.fetchall())'''
#drop table
'''query="DROP TABLE STUDENT"
cursorobj.execute(query)
dataBase.commit()'''
#check
query="SHOW TABLES"
cursorobj.execute(query)
print(cursorobj.fetchall())
dataBase.close()