Skip to content

Commit 2692388

Browse files
author
david lambert
committed
Overhauled SQLite.py, SQLite_import.py, & SQLite_schema_from_file.py.
1 parent 70ea5d8 commit 2692388

24 files changed

+4054
-65
lines changed

DBClient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def run_sql(self) -> (list, list, int):
135135

136136
# Handle SQL results.
137137
if sql_type in {'INSERT', 'UPDATE', 'DELETE'}:
138-
self.cursor.commit()
138+
self.cursor.connection.commit()
139139
elif sql_type != 'SELECT':
140140
print('Not a CRUD statement!')
141141
elif sql_type == 'SELECT':

DBInstance.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __init__(self,
9090

9191
# Get the library's primary parameter style.
9292
print('Parameter style "{}".'.format(self.db_lib_obj.paramstyle))
93-
# paramstyle = 'named': cx_Oracle. Option for sqlite3 & psycopg2.
93+
# paramstyle = 'named': oracledb. Option for sqlite3 & psycopg2.
9494
# paramstyle = 'qmark': sqlite3 and pyodbc.
9595
# paramstyle = 'pyformat': pymysql and psycopg2.
9696

@@ -116,7 +116,9 @@ def __init__(self,
116116
else:
117117
args = (self.hostname, self.username, self.password,
118118
self.instance, self.port_num)
119-
self.connection = self.db_lib_obj.connect(*args)
119+
# self.connection = self.db_lib_obj.connect(*args)
120+
self.connection = self.db_lib_obj.connect(host=self.hostname, user=self.username, password=self.password, db=self.instance,
121+
port=self.port_num)
120122
print('Successfully connected to database.')
121123
except self.db_lib_obj.Error:
122124
print_stacktrace()
@@ -300,7 +302,7 @@ def get_db_connection_string(self) -> str:
300302
db_software_version (str): database software version.
301303
"""
302304
z = ''
303-
if self.db_lib_name == c.CX_ORACLE:
305+
if self.db_lib_name == c.ORACLEDB:
304306
z = '{}/{}@{}:{}/{}'
305307
elif self.db_lib_name == c.PSYCOPG2:
306308
z = "user='{}' password='{}' host='{}' port='{}' dbname='{}'"
@@ -323,6 +325,7 @@ def get_db_connection_string(self) -> str:
323325
if self.db_type in {ORACLE, POSTGRESQL, SQLSERVER}:
324326
z = z.format(self.username, self.password, self.hostname,
325327
self.port_num, self.instance)
328+
print(z)
326329
elif self.db_type in c.FILE_DATABASES:
327330
z = z.format(self.db_path)
328331
elif self.db_type == MYSQL:

UniversalClient.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
DATE: Jul 9, 2020
1313
1414
For more information, see README.rst.
15+
ACCESS OK
16+
SQLITE3 OK
17+
MYSQL NOT OK
18+
POSTGRES OK
19+
ORACLE OK
1520
"""
1621
# -------- IMPORTS
1722

@@ -30,11 +35,17 @@
3035
ACCESS: '',
3136
MYSQL: 'root',
3237
ORACLE: 'ds2',
33-
POSTGRESQL: 'root',
38+
POSTGRESQL: 'ds2',
3439
SQLITE: '',
3540
SQLSERVER: 'sa'}
3641

37-
sample_password = 'password'
42+
sample_password = {
43+
ACCESS: '',
44+
MYSQL: 'ds2',
45+
ORACLE: 'ds2',
46+
POSTGRESQL: 'ds2',
47+
SQLITE: '',
48+
SQLSERVER: 'Pa$$w0rd'}
3849

3950
# VirtualBox Guests use NAT.
4051
sample_hostname = '127.0.0.1'
@@ -50,10 +61,10 @@
5061
sample_instance = {
5162
ACCESS: '',
5263
MYSQL: 'DS2',
53-
ORACLE: 'ORCL',
64+
ORACLE: 'XE',
5465
POSTGRESQL: 'ds2',
5566
SQLITE: '',
56-
SQLSERVER: 'DS2'}
67+
SQLSERVER: 'DS3'}
5768

5869
home = 'C:\\Coding\\PyCharm\\projects\\Python-Universal-DB-Client\\'
5970
sample_db_path = {
@@ -80,14 +91,29 @@ def main() -> None:
8091
os, py_version_major, py_version_minor = os_python_version_info()
8192

8293
# GET DATABASE CONNECTION INFO TO USE.
83-
db_type1 = SQLITE
84-
94+
prompt = "Enter the number for the database type you want:"
95+
for choice_num, choice in enumerate(c.DB_TYPES):
96+
prompt += f"\n{choice_num}: {choice}"
97+
prompt += ": "
98+
while True:
99+
db_type1 = input(prompt)
100+
if db_type1.isdigit():
101+
db_type1 = int(db_type1)
102+
if 0 <= db_type1 <= len(c.DB_TYPES)-1:
103+
db_type1 = c.DB_TYPES[db_type1]
104+
break
105+
else:
106+
print(f"Invalid integer choice: {db_type1}")
107+
else:
108+
print("Only integers allowed.")
85109
if db_type1 not in c.DB_TYPES:
86110
print('UNKNOWN DATABASE TYPE.')
87111
exit(1)
112+
print(f"You chose '{db_type1}'.")
113+
88114
db_path1 = sample_db_path[db_type1]
89115
username1 = sample_username[db_type1]
90-
password1 = sample_password
116+
password1 = sample_password[db_type1]
91117
hostname1 = sample_hostname
92118
port_num1 = sample_port_num[db_type1]
93119
instance1 = sample_instance[db_type1]
@@ -224,7 +250,7 @@ def main() -> None:
224250
# MS Access does not support bind variables/parameterization.
225251
query2 = query.format("'CHEVY FOSTER'", "35.0", terminator='')
226252
elif paramstyle2 == c.NAMED:
227-
# oracle/cx_Oracle and sqlite/sqlite3.
253+
# oracle/oracledb and sqlite/sqlite3.
228254
query2 = query.format(':actor', ':price', terminator='')
229255
bind_vars2 = {'actor': 'CHEVY FOSTER', 'price': 35.0}
230256
elif paramstyle2 == c.PYFORMAT:

constants.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434

3535
# DATABASE LIBRARIES.
3636

37-
CX_ORACLE = 'cx_Oracle'
37+
ORACLEDB = 'oracledb'
3838
PSYCOPG2 = 'psycopg2'
3939
PYMYSQL = 'pymysql'
4040
PYODBC = 'pyodbc'
4141
SQLITE3 = 'sqlite3'
4242
LIB_NAME_FOR_DB = {
4343
ACCESS: PYODBC,
4444
MYSQL: PYMYSQL,
45-
ORACLE: CX_ORACLE,
45+
ORACLE: ORACLEDB,
4646
POSTGRESQL: PSYCOPG2,
4747
SQLITE: SQLITE3,
4848
SQLSERVER: PYODBC}
@@ -63,7 +63,7 @@
6363
QMARK = 'qmark'
6464
NOBINDVARS = 'nobindvars'
6565
PARAMSTYLE_FOR_LIB = {
66-
CX_ORACLE: NAMED,
66+
ORACLEDB: NAMED,
6767
PSYCOPG2: PYFORMAT,
6868
PYMYSQL: PYFORMAT,
6969
PYODBC: QMARK,

databases/ds2.sqlite3

-4.78 MB
Binary file not shown.

miniSQLite.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

mini_versions/MySQL.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pymysql
2+
3+
# query2 = query.format("%(actor)s", "%(price)s", terminator='')
4+
# bind_vars2 = {'actor': 'CHEVY FOSTER', 'price': 35.0}
5+
6+
# MySQL database login and location information.
7+
username = "root"
8+
password = "password"
9+
hostname = "127.0.0.1"
10+
port_num = 3306
11+
instance = "jingw"
12+
13+
# Print properties of the mysql.connector library.
14+
print(f"\nmysql.connector library version: {pymysql.__version__}")
15+
print(f"mysql.connector parameter style ('named', 'qmark', or 'pyformat'): {mysql.connector.paramstyle}")
16+
17+
# Make connection to database.
18+
connection = pymysql.connect(user=username, password=password, host=hostname, port=port_num, database=instance)
19+
20+
# Create cursor.
21+
cursor = connection.cursor()
22+
23+
sql = "delete from item where iid = 2"
24+
cursor.execute(sql)
25+
26+
rows = cursor.fetchall()
27+
print("Rows in result set:")
28+
for row in rows:
29+
print(row)
30+
31+
# Commit any remaining inserts.
32+
connection.commit()
33+
34+
# Finish up.
35+
cursor.close()
36+
connection.close()
37+
38+
print("All Done.")

0 commit comments

Comments
 (0)