-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_base.py
76 lines (55 loc) · 2.31 KB
/
data_base.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
import sqlite3
# connect to data base or open the data base
conn = sqlite3.connect('my_db.db')
print("Opened database successfully")
# to create database table
# conn.execute('''CREATE TABLE COMPANY
# (ID INT PRIMARY KEY NOT NULL,
# NAME TEXT NOT NULL,
# AGE INT NOT NULL,
# ADDRESS CHAR(50),
# SALARY REAL);''')
# print("Table created successfully")
# to insert data into the the table
# conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
# VALUES (1, 'Paul', 32, 'California', 20000.00 )")
# conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
# VALUES (2, 'Allen', 25, 'Texas', 15000.00 )")
# conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
# VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )")
# conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
# VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )")
# conn.commit()
# print ("Records created successfully")
# to fetch or print data from data base
cursor = conn.execute("SELECT id, name, address, salary from COMPANY")
for row in cursor:
print( "ID = ", row[0])
print( "NAME = ", row[1])
print( "ADDRESS = ", row[2])
print( "SALARY = ", row[3], "\n")
print( "Operation done successfully")
# to update dat base
# conn.execute("UPDATE COMPANY set SALARY = 25000.00 where ID = 1")
# conn.commit()
# print ("Total number of rows updated :", conn.total_changes)
# cursor = conn.execute("SELECT id, name, address, salary from COMPANY")
# for row in cursor:
# print( "ID = ", row[0])
# print( "NAME = ", row[1])
# print( "ADDRESS = ", row[2])
# print( "SALARY = ", row[3], "\n")
# print( "Operation done successfully")
# to delete from data base or table
# conn.execute("DELETE from COMPANY where ID = 2;")
# conn.commit()
# print ("Total number of rows deleted :", conn.total_changes)
# cursor = conn.execute("SELECT id, name, address, salary from COMPANY")
# for row in cursor:
# print( "ID = ", row[0])
# print( "NAME = ", row[1])
# print( "ADDRESS = ", row[2])
# print( "SALARY = ", row[3], "\n")
# print( "Operation done successfully")
# close the data base or connnection
conn.close()