Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
soitool
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
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bachelor-paa-bittet
soitool
Commits
4278443b
Commit
4278443b
authored
5 years ago
by
Anders H. Rebner
Browse files
Options
Downloads
Patches
Plain Diff
#5 Basic database setup ferdig
parent
765aea70
No related branches found
No related tags found
1 merge request
!10
Database setup
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
scripts/.pylintrc
+1
-0
1 addition, 0 deletions
scripts/.pylintrc
soitool/database.py
+60
-2
60 additions, 2 deletions
soitool/database.py
with
61 additions
and
2 deletions
scripts/.pylintrc
+
1
−
0
View file @
4278443b
...
...
@@ -409,6 +409,7 @@ good-names=app,
k,
ex,
Run,
f,
_
# Include a hint for the correct naming format with invalid-name.
...
...
This diff is collapsed.
Click to expand it.
soitool/database.py
+
60
−
2
View file @
4278443b
...
...
@@ -2,13 +2,16 @@
import
os
import
sqlite3
# Set name and path to (future) database
DBNAME
=
'
Database
'
CURDIR
=
os
.
path
.
dirname
(
__file__
)
DBPATH
=
os
.
path
.
join
(
CURDIR
,
DBNAME
)
# DDL-statements for creating tables
CODEBOOK
=
'
CREATE TABLE CodeBook(Word VARCHAR PRIMARY KEY, Category VARCHAR, Type int DEFAULT 0)
'
CATEGORYWORDS
=
'
CREATE TABLE CategoryWords(Word VARCHAR PRIMARY KEY, Category VARCHAR)
'
CODEBOOK
=
'
CREATE TABLE CodeBook
'
\
'
(Word VARCHAR PRIMARY KEY, Category VARCHAR, Type int DEFAULT 0)
'
CATEGORYWORDS
=
'
CREATE TABLE CategoryWords
'
\
'
(Word VARCHAR PRIMARY KEY, Category VARCHAR)
'
BYHEART
=
'
CREATE TABLE ByHeart(Word VARCHAR PRIMARY KEY)
'
...
...
@@ -23,11 +26,18 @@ class Database():
print
(
'
connecting to existing DB.
'
)
self
.
conn
=
sqlite3
.
connect
(
DBPATH
)
print
(
'
DB-connection established.
'
)
self
.
conn
.
row_factory
=
sqlite3
.
Row
# Enables row['columnName']
else
:
print
(
'
Creating new DB.
'
)
self
.
conn
=
sqlite3
.
connect
(
DBPATH
)
self
.
create_tables
()
print
(
'
DB created.
'
)
self
.
fill_tables
()
print
(
'
Tables filled with data.
'
)
self
.
conn
.
row_factory
=
sqlite3
.
Row
# Enables row['columnName']
self
.
categories
=
self
.
get_categories
()
def
create_tables
(
self
):
"""
Create tables Codebook, CategoryWords and ByHeart.
"""
...
...
@@ -37,3 +47,51 @@ class Database():
self
.
conn
.
execute
(
stmt
)
self
.
conn
.
commit
()
def
fill_tables
(
self
):
"""
Fill tables with testdata.
"""
# Read from ByHeart
f
=
open
(
os
.
path
.
join
(
CURDIR
,
"
testdata/ByHeart.txt
"
),
"
r
"
)
# Loop through words in file and insert them into ByHeart-table
for
word
in
f
:
stmt
=
'
INSERT INTO ByHeart(Word) VALUES(?)
'
self
.
conn
.
execute
(
stmt
,
(
word
[:
-
1
],))
# Remove \n
# Read from CategoryWords
file_path
=
os
.
path
.
join
(
CURDIR
,
"
testdata/CategoryWords.txt
"
)
f
=
open
(
file_path
,
"
r
"
,
encoding
=
'
utf-8
'
)
no_of_categories
=
int
(
f
.
readline
()[
0
])
for
_
in
range
(
no_of_categories
):
# Loop through all categories
line
=
f
.
readline
().
split
(
"
,
"
)
category
=
line
[
0
]
no_of_words
=
int
(
line
[
1
][:
-
1
])
# Remove \n
for
_
in
range
(
no_of_words
):
# Loop through
word
=
f
.
readline
()[:
-
1
]
# Remove \n
stmt
=
'
INSERT INTO CategoryWords(Word, Category) VALUES(?, ?)
'
self
.
conn
.
execute
(
stmt
,
(
word
,
category
,))
self
.
conn
.
commit
()
def
get_categories
(
self
):
"""
Retrieve all categories from table CategoryWords.
Returns
-------
List of strings
Categories
"""
stmt
=
'
SELECT Category FROM CategoryWords GROUP BY Category
'
test
=
self
.
conn
.
execute
(
stmt
)
categories
=
[]
for
row
in
test
:
categories
.
append
(
row
[
'
Category
'
])
return
categories
# temp = Database()
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