Skip to content

Commit b7d056a

Browse files
Added two files.
0 parents  commit b7d056a

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

Database_python_connection.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import sqlite3
2+
import os
3+
from sqlite3 import Error
4+
5+
def create_connection(Education):
6+
conn = None
7+
try:
8+
conn = sqlite3.connect(Education)
9+
except Error as e:
10+
print(e)
11+
12+
return conn
13+
14+
# col_name
15+
def select_task(conn,table_name,col_name,where_clause):
16+
cur = conn.cursor()
17+
#new string
18+
str_query = ""
19+
20+
str_query = str_query + "SELECT "
21+
print("After adding select keyword ",str_query)
22+
23+
#string for all the relevent columns of the table:
24+
str_column_list = ""
25+
# SELECT Roll_Number,a,b,c FROM Student
26+
for i in range(len(col_name)):
27+
str_column_list = str_column_list + col_name[i]
28+
print(str_column_list)
29+
if i != len(col_name)-1:
30+
str_column_list = str_column_list + ","
31+
32+
print( " Column Names are ",str_column_list)
33+
34+
str_query = str_query + str_column_list
35+
print( " Now Query is ",str_query)
36+
37+
str_query = str_query + " FROM " + table_name + " "
38+
39+
print( " Now Query is ",str_query)
40+
41+
if where_clause != "":
42+
str_query = str_query + " WHERE " + where_clause
43+
44+
print( " Now Query is ",str_query)
45+
cur.execute(str_query)
46+
rows = cur.fetchall()
47+
for row in rows:
48+
print(row)
49+
50+
def delete_task(conn,table_name,where_clause):
51+
cur = conn.cursor()
52+
#DELETE FROM Student where Subject_ID=2
53+
str_query = ""
54+
if where_clause != "":
55+
str_query = str_query + "DELETE FROM " + table_name + " WHERE "+ where_clause
56+
else:
57+
str_query = str_query + "DELETE FROM " + table_name
58+
59+
str_query="Select * FROM "+table_name
60+
print( " Now Delete Query is ",str_query)
61+
62+
cur.execute(str_query)
63+
64+
rows = cur.fetchall()
65+
66+
for row in rows:
67+
print(row)
68+
69+
# update tb set c1 = c1_val , c2 = c2_val WHERE c_name = c_name_value
70+
def update_task(conn,table_name,col_name,data,where_clause):
71+
cur = conn.cursor()
72+
str_query = ""
73+
str_query = str_query + "update "+ table_name+ " set "
74+
75+
for i in range(len(col_name)):
76+
str_query = str_query + col_name[i] + " = '"+ data[i] + "'"
77+
if i != len(col_name)-1:
78+
str_query = str_query + ","
79+
80+
if where_clause != "":
81+
str_query = str_query + " WHERE "+ where_clause
82+
#str = "update Subject set branch='computer science' where subject_id = 3"
83+
print( " Now Update Query is ",str_query)
84+
85+
cur.execute(str_query)
86+
87+
rows = cur.fetchall()
88+
89+
for row in rows:
90+
print(row)
91+
92+
93+
94+
def main():
95+
database = r"Education.db"
96+
print("Current Path : ",os.getcwd())
97+
conn = create_connection(database)
98+
with conn:
99+
print("1. Query task")
100+
101+
col_name = ['Roll_Number','Name']
102+
#select_task(conn,'Student',col_name,"Name='Ram'")
103+
select_task(conn,'Student',col_name,"")
104+
105+
#col_name = ['Branch','Subject_Name']
106+
delete_task(conn,'Subject',"Subject_ID=4")
107+
108+
col_name = ['Name','Address','Id_Proof']
109+
data_send = ['Astha','Gorakhpur','PAN']
110+
update_task(conn,'Student',col_name,data_send,"Roll_Number=3")
111+
112+
col_name = ['Name','Roll_Number','Address','Phone_Number','Id_Proof']
113+
#select_task(conn,'Student',col_name,"Name='Ram'")
114+
select_task(conn,'Student',col_name,"")
115+
116+
117+
if __name__ == '__main__':
118+
main()

Education.db

24 KB
Binary file not shown.

0 commit comments

Comments
 (0)