@HUDSON_REYREY/

Python Database API

Python

No description

fork
loading
Files
  • main.py
  • TEST.db
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'''
Python Database API
----------------------------------
How to connect to Python database?

Python Database API (DB-API) is a standard interface to interact with various databases.
DB-API supports connecting to different database servers such as Microsoft SQL Server 2000, PostgreSQL, Oracle.
Different DB API’s are used for accessing different databases. Hence a programmer has to install DB API corresponding to the database one is working with.


Working with a Database
-----------------------------------------------------
Working with a database includes the following steps:

-Importing the corresponding DB-API module.

-Acquiring a connection with the database.

-Executing SQL statements and stored procedures.

-Closing the connection


Creating Tables
------------------------------------------------------
You have connected to the database. Now, how do you create your tables in the database?

In this topic, you will learn using sqlite3, DB API for SQLite database.
sqlite3 is available by default in Python standard library and hence there is no need of installing it separately.
SQLite is a very lightweight database. You can connect to it directly, with minimal settings.

Creating a Sample Table
-------------------------------------
Example 1
-------------------------------------
'''
import sqlite3



# establishing  a database connection

con = sqlite3.connect('TEST.db')



# preparing a cursor object

cursor = con.cursor()



# preparing sql statements

sql1 = 'DROP TABLE IF EXISTS EMPLOYEE'


sql2 = '''
       CREATE TABLE EMPLOYEE (

       EMPID INT(6) NOT NULL,

       NAME CHAR(20) NOT NULL,

       AGE INT,

       SEX CHAR(1),

       INCOME FLOAT

       )
      '''



# executing sql statements

#cursor.execute(sql1)

cursor.execute(sql2)



# closing the connection

con.close()

#You can run and verify if TEST.db file got created at the specified location.


'''
Inserting Data
-----------------------------------
Data stored in Python variables can be inserted into database tables.

Data is passed to SQL statements through parameter substitution.

Single rows are inserted using execute and multiple rows using executeMany method of created cursor object.

Inserting a Sample Data
------------------------
Example 1
------------------------
'''

import sqlite3



# establishing the connection

con = sqlite3.connect('D:\\TEST.db')



# preparing a cursor object

cursor = con.cursor()



# preparing sql statement

rec = (456789, 'Frodo', 45, 'M', 100000.00)

sql = '''

       INSERT INTO EMPLOYEE VALUES ( ?, ?, ?, ?, ?)

      '''


# executing sql statement using try ... except blocks

try:

    cursor.execute(sql, rec)

    con.commit()

except Exception as e:

    print("Error Message :", str(e))

    con.rollback()



# closing the database connection

con.close()

#A single record is inserted into table EMPLOYEE.
#-------------------------------------------------


#Example 2
#------------------------------


import sqlite3



con = sqlite3.connect('D:\\TEST.db')



cursor = con.cursor()



# preparing sql statement

records = [

    (123456, 'John', 25, 'M', 50000.00),

    (234651, 'Juli', 35, 'F', 75000.00),

    (345121, 'Fred', 48, 'M', 125000.00),

    (562412, 'Rosy', 28, 'F', 52000.00)

    ]

sql = '''

       INSERT INTO EMPLOYEE VALUES ( ?, ?, ?, ?, ?)

      '''
# executing sql statement using try ... except blocks

try:

    cursor.executemany(sql, records)

    con.commit()

except Exception as e:

    print("Error Message :", str(e))

    con.rollback()



# closing the database connection

con.close()
#Four more records are inserted into table EMPLOYEE.