Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Advanced-Software-Development
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Terence2.Frayne@live.uwe.ac.uk
Advanced-Software-Development
Commits
1118a8ae
Commit
1118a8ae
authored
8 months ago
by
nathan
Browse files
Options
Downloads
Patches
Plain Diff
added more dbfuncs, note: i cant debug any of it untill we have adb setup.
parent
4dbe65ba
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/dbfunc.py
+63
-5
63 additions, 5 deletions
src/dbfunc.py
src/staffUser.py
+4
-4
4 additions, 4 deletions
src/staffUser.py
with
67 additions
and
9 deletions
src/dbfunc.py
+
63
−
5
View file @
1118a8ae
import
logging
import
mysql.connector
import
logging
,
json
,
mysql
.
connector
from
mysql.connector
import
errorcode
# Vars
...
...
@@ -42,7 +41,7 @@ def getConnection(db=""):
logging
.
error
(
"
Connected to server, without database.
"
)
return
conn
def
selectFromTbl
(
table
,
dbname
,
*
args
):
def
selectFromTbl
(
table
,
dbname
,
*
args
)
->
json
:
logging
.
debug
(
f
"
running slect statement with values: tablename=
'
{
table
}
''
, dbname=
'
{
dbname
}
''
,
{
args
}
"
)
conn
=
getConnection
(
db
=
dbname
)
if
conn
!=
None
:
...
...
@@ -57,13 +56,72 @@ def selectFromTbl(table, dbname, *args):
dbcursor
.
execute
(
f
"
USE
{
dbname
}
"
)
dbcursor
.
execute
(
SELECT_STATEMENT
)
logging
.
info
(
"
SELECT statement executed successfully.
"
)
data
Out
=
dbcursor
.
fetchall
()
logging
.
debug
(
f
"
dataOut:
{
data
Out
}
"
)
data
=
dbcursor
.
fetchall
()
logging
.
debug
(
f
"
dataOut:
{
data
}
"
)
dbcursor
.
close
()
conn
.
close
()
data
=
json
.
dumps
(
data
)
return
data
else
:
logging
.
error
(
"
conn not connected
"
)
raise
ConnectionError
()
else
:
logging
.
error
(
"
conn returned NoneType
"
)
raise
ConnectionAbortedError
()
def
selectFromTblWhere
(
table
,
dbname
,
condition
,
*
args
)
->
json
:
logging
.
debug
(
f
"
running select statement with values: tablename=
'
{
table
}
''
, dbname=
'
{
dbname
}
''
,
{
args
}
"
)
conn
=
getConnection
(
db
=
dbname
)
if
conn
==
None
:
logging
.
error
(
"
conn returned NoneType
"
)
raise
ConnectionAbortedError
()
if
conn
.
is_connected
():
SELECT_statement
=
"
SELECT
"
for
arg
in
args
:
SELECT_statement
.
append
(
f
"
{
arg
}
,
"
)
SELECT_statement
.
removesuffix
(
"
,
"
)
SELECT_statement
.
append
(
f
"
from
{
table
}
;
"
)
dbcursor
=
conn
.
cursor
()
dbcursor
.
execute
(
f
"
USE
{
dbname
}
"
)
dbcursor
.
execute
(
SELECT_statement
)
logging
.
info
(
"
SELECT statement executed successfully.
"
)
data
=
dbcursor
.
fetchall
()
logging
.
debug
(
f
"
dataOut:
{
data
}
"
)
dbcursor
.
close
()
conn
.
close
()
data
=
json
.
dumps
(
data
)
return
data
else
:
logging
.
error
(
"
conn not connected
"
)
raise
ConnectionError
()
def
saveToTbl
(
table
,
dbname
,
jsonData
):
logging
.
debug
(
f
"
running update statment with values: tablename=
'
{
table
}
'
, dbname=
'
{
dbname
}
'
, data=
'
{
jsonData
}
'"
)
conn
=
getConnection
(
db
=
dbname
)
if
conn
==
None
:
logging
.
error
(
"
conn returned NoneType
"
)
raise
ConnectionAbortedError
()
if
conn
.
is_connected
():
logging
.
debug
(
"
MySQL Connection is established.
"
)
INSERT_statement
=
f
"
INSERT INTO
{
table
}
(
"
dictData
=
json
.
loads
(
jsonData
)
keys
=
""
values
=
""
for
key
,
value
in
dictData
.
items
():
keys
.
append
(
f
"
{
key
}
,
"
)
values
.
append
(
f
"
{
value
}
,
"
)
INSERT_statement
.
append
(
f
"
{
keys
.
removesuffix
(
"
,
"
)
}
) VALUES (
{
values
.
removesuffix
(
"
,
"
)
}
);
"
)
dbcursor
=
conn
.
cursor
()
dbcursor
.
execute
(
INSERT_statement
)
conn
.
commit
()
dbcursor
.
close
()
conn
.
close
()
logging
.
debug
(
"
INSERT query executed with no issues.
"
)
else
:
logging
.
error
(
"
conn not connected
"
)
raise
ConnectionError
()
This diff is collapsed.
Click to expand it.
src/staffUser.py
+
4
−
4
View file @
1118a8ae
import
logging
import
random
import
logging
,
json
from
.dbfunc
import
*
from
.cinemaObj
import
Cinema
...
...
@@ -55,8 +55,8 @@ class StaffUser(object):
def
manageBooking
(
self
):
raise
NotImplementedError
()
def
updateFromDataBase
(
self
)
->
None
:
# call dbFunc
def
updateFromDataBase
(
self
,
userID
)
->
None
:
selectFromTblWhere
(
"
tblUsers
"
,
""
,
f
"
user_id=
{
userID
}
"
,
"
*
"
)
# gets dbData in form of json
# convert to dict?
# update self data with new values.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment