Skip to content
Snippets Groups Projects
Commit 6a1ae5f9 authored by Jonas Leder's avatar Jonas Leder
Browse files

add coments on import file

parent b6616ec1
No related branches found
No related tags found
No related merge requests found
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment