-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database_python_connection.py
111 lines (84 loc) · 2.94 KB
/
Database_python_connection.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import sqlite3
import os
from sqlite3 import Error
# Used to create connection with Database file
def create_connection(Education):
conn = None
try:
conn = sqlite3.connect(Education)
except Error as e:
print(e)
return conn
# Used to Select the Column Names from table
def select_task(conn,table_name,col_name,where_clause):
cur = conn.cursor()
str_query = "SELECT "
print("After adding select keyword ",str_query)
#string for all the relevent columns of the table:
str_column_list = ""
# SELECT Roll_Number,a,b,c FROM Student
for i in range(len(col_name)):
str_column_list = str_column_list + col_name[i]
print(str_column_list)
if i != len(col_name)-1:
str_column_list = str_column_list + ","
print( " Column Names are ",str_column_list)
str_query = str_query + str_column_list
print( " Now Query is ",str_query)
str_query = str_query + " FROM " + table_name + " "
print( " Now Query is ",str_query)
if where_clause != "":
str_query = str_query + " WHERE " + where_clause
print( " Now Query is ",str_query)
cur.execute(str_query)
rows = cur.fetchall()
for row in rows:
print(row)
def delete_task(conn,table_name,where_clause):
cur = conn.cursor()
#DELETE FROM Student where Subject_ID=2
str_query = ""
if where_clause != "":
str_query = str_query + "DELETE FROM " + table_name + " WHERE "+ where_clause
else:
str_query = str_query + "DELETE FROM " + table_name
print( " Now Delete Query is ",str_query)
cur.execute(str_query)
rows = cur.fetchall()
for row in rows:
print(row)
# update tb set c1 = c1_val , c2 = c2_val WHERE c_name = c_name_value
def update_task(conn,table_name,col_name,data,where_clause):
cur = conn.cursor()
str_query = "update "+ table_name+ " set "
for i in range(len(col_name)):
str_query = str_query + col_name[i] + " = '"+ data[i] + "'"
if i != len(col_name)-1:
str_query = str_query + ","
if where_clause != "":
str_query = str_query + " WHERE "+ where_clause
#str = "update Subject set branch='computer science' where subject_id = 3"
print( " Now Update Query is ",str_query)
cur.execute(str_query)
rows = cur.fetchall()
for row in rows:
print(row)
def main():
database = r"Education.db"
print("Current Path : ",os.getcwd())
conn = create_connection(database)
with conn:
print("1. Query task")
col_name = ['Roll_Number','Name']
#select_task(conn,'Student',col_name,"Name='Ram'")
select_task(conn,'Student',col_name,"")
#col_name = ['Branch','Subject_Name']
delete_task(conn,'Subject',"Subject_ID=4")
col_name = ['Name','Address','Id_Proof']
data_send = ['Astha','Gorakhpur','PAN']
update_task(conn,'Student',col_name,data_send,"Roll_Number=3")
col_name = ['Name','Roll_Number','Address','Phone_Number','Id_Proof']
#select_task(conn,'Student',col_name,"Name='Ram'")
select_task(conn,'Student',col_name,"")
if __name__ == '__main__':
main()