- Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathChapter_16th_sql_example2.py
More file actions
Latest commit
132 lines (110 loc) · 3.66 KB
/
Copy pathChapter_16th_sql_example2.py
File metadata and controls
132 lines (110 loc) · 3.66 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 7 14:25:45 2020
@author: sana.rasheed
"""
### Chapter 16th - SQLite
# Example 2
# Functions implementated for quick DB connection and Table creations
importsqlite3
fromsqlite3importError
# Function to open DB connection
defcreate_connection(db_file):
""" create a database connection to the SQLite database specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn=None
try:
conn=sqlite3.connect(db_file)
print("The SQLite connection is connected")
returnconn
exceptErrorase:
print(e)
returnconn
# Function to close DB connection
defclose_connection(conn):
""" close database connection to the SQLite database specified by db_file
:param db_file: database file
"""
if (conn):
conn.close()
print("The SQLite connection is closed")
# Function to create new table in DB
defcreate_table_in_db(conn, create_table_sql):
""" create a table from the create_table_sql statement
:param conn: Connection object
:param create_table_sql: a CREATE TABLE statement
:return:
"""
try:
c=conn.cursor()
c.execute(create_table_sql)
exceptErrorase:
print(e)
# Create STUDENT Table
defcreate_table():
database=r"C:\\tempdir\database.db"
sql_create_projects_table=""" CREATE TABLE IF NOT EXISTS STUDENTS (
id integer PRIMARY KEY,
name text NOT NULL,
gpa integer,
admission_date text
); """
# create a database connection
conn=create_connection(database)
# create tables
ifconnisnotNone:
# create projects table
create_table_in_db(conn, sql_create_projects_table)
else:
print("Error! cannot create the database connection.")
close_connection(conn)
# main execution to create table
create_table()
# ======================================================
# Add student data in table
defadd_student(conn, student):
"""
Create a new student entry into the student table
:param conn:
:param student:
:return: student id
"""
sql=''' INSERT INTO STUDENTS(name,gpa,admission_date)
VALUES(?,?,?) '''
cur=conn.cursor()
cur.execute(sql, student)
returncur.lastrowid
# main function to add student in table
defmain_function():
database=r"C:\\tempdir\database.db"
# create a database connection
conn=create_connection(database)
withconn:
# add a new student
student= ('Alan', 1.9, '2019-1-30');
student_id=add_student(conn, student)
print('The Student ID:', student_id)
close_connection(conn)
# Execute main funtion to add student 'Alan' in table
main_function()
# ==============================================================
### READ TABLE
database=r"C:\\tempdir\database.db"
#create a database connection
conn=create_connection(database)
try:
cursor=conn.cursor()
table_name="STUDENTS"
sql_string="SELECT * from "+table_name
sqlite_select_query=sql_string
cursor.execute(sqlite_select_query)
records=cursor.fetchall()
print("Total rows are: ", len(records))
cursor.close()
exceptsqlite3.Erroraserror:
print("Failed to read data from sqlite table", error)
finally:
close_connection(conn)
print(records)