Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
U
url_shorter_docker
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
Service Desk
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
Jonas Leder
url_shorter_docker
Commits
6a1ae5f9
Commit
6a1ae5f9
authored
5 years ago
by
Jonas Leder
Browse files
Options
Downloads
Patches
Plain Diff
add coments on import file
parent
b6616ec1
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
import.py
+9
-10
9 additions, 10 deletions
import.py
with
9 additions
and
10 deletions
import.py
+
9
−
10
View file @
6a1ae5f9
import
sqlite3
import
sqlite3
import
os
import
os
def
table_check
():
def
table_check
():
#Check if database exists
create_table
=
"""
create_table
=
"""
CREATE TABLE WEB_URL(
CREATE TABLE WEB_URL(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
ID INTEGER PRIMARY KEY AUTOINCREMENT,
...
@@ -19,19 +19,18 @@ table_check()
...
@@ -19,19 +19,18 @@ table_check()
with
sqlite3
.
connect
(
'
db/urls.db
'
)
as
conn
:
with
sqlite3
.
connect
(
'
db/urls.db
'
)
as
conn
:
cursor
=
conn
.
cursor
()
cursor
=
conn
.
cursor
()
try
:
try
:
file
=
open
(
"
import.csv
"
,
"
r
"
).
readlines
()
file
=
open
(
"
import.csv
"
,
"
r
"
).
readlines
()
#try opening file and read all lines
except
:
except
:
print
(
"
no file for import found
"
)
print
(
"
no file for import found
"
)
#If open fails, there was no file.
exit
()
exit
()
entries
=
len
(
file
)
entries
=
len
(
file
)
#Count the entries (for the output in the for loop)
counter
=
1
counter
=
1
for
lines
in
file
:
for
lines
in
file
:
print
(
"
Importing
"
+
str
(
counter
)
+
"
from
"
+
str
(
entries
))
print
(
"
Importing
"
+
str
(
counter
)
+
"
from
"
+
str
(
entries
))
#Make a progress message (mormaly unnecessary, because import is to quick (<1s))
SHORT_URL
=
lines
.
split
(
"
;
"
)[
0
].
replace
(
"
\n
"
,
""
).
replace
(
"
\r
"
,
""
)
SHORT_URL
=
lines
.
split
(
"
;
"
)[
0
].
replace
(
"
\n
"
,
""
).
replace
(
"
\r
"
,
""
)
#Split the CSV at the ";" then use the first one and replace all linebreaks
LONG_URL
=
lines
.
split
(
"
;
"
)[
1
].
replace
(
"
\n
"
,
""
).
replace
(
"
\r
"
,
""
)
LONG_URL
=
lines
.
split
(
"
;
"
)[
1
].
replace
(
"
\n
"
,
""
).
replace
(
"
\r
"
,
""
)
#Split the CSV at the ";" then use the seccond one and replace all linebreaks
res
=
cursor
.
execute
(
res
=
cursor
.
execute
(
#Insert the data in the SQL table
'
INSERT INTO WEB_URL (LONG_URL, SHORT_URL) VALUES (?, ?)
'
,
'
INSERT INTO WEB_URL (LONG_URL, SHORT_URL) VALUES (?, ?)
'
,
[
LONG_URL
,
SHORT_URL
]
[
LONG_URL
,
SHORT_URL
]
)
)
counter
=
counter
+
1
counter
=
counter
+
1
#Add 1 to counter, for progress
os
.
system
(
"
exit
"
)
\ No newline at end of file
\ No newline at end of file
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