This repository has been archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
02-import-data.py
executable file
·85 lines (62 loc) · 2.15 KB
/
02-import-data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
# -*- coding: UTF-8; -*-
'''
Put verda2.txt in mysql db
'''
import string, sys # MySQLdb
import mysql.connector # sudo pip install mysql-connector
def get_db_cursor ():
conn=mysql.connector.connect(host = "10.223.68.17",
user = "pyyrlib",
passwd = "pyyrlib",
db = "pyyrlib")
return conn, conn.cursor ()
def clear_db (cursor, table):
query = "delete from " + table
cursor.execute(query)
def insert_row_countries (cursor, table, fields):
query = "INSERT INTO " + table + " (countrycode, countryname) VALUES ( "
for i in range(0, 2):
if 0 != i:
query += ", "
query += "'" + all_lower(fields[i]) + "'"
query += " ) ON DUPLICATE KEY UPDATE countryname = '" + fields[1] + "' ;"
print (query)
return cursor.execute(query)
def insert_row_verda (cursor, conn, table, fields):
query = "INSERT INTO " + table + " (countryid, placename, xml) VALUES ( "
for i in [0, 1, 3]:
if 0 != i:
query += ", "
if 0 == i:
query += " (select countryid from countries where countrycode = '" + all_lower(fields[0]) + "' ) "
elif 2 == i:
continue
else:
#query += "'" + conn.fields[i].replace(' ', '') + "'"
query += '"' + fields[i].replace(' ', '') + '"'
query += " ) ;"
print (query)
return cursor.execute(query)
def process_file_countries (cursor):
fd = open( "/home/larsfp/git/pyyrlib/countries.txt" )
content = fd.readline()
while (content != "" ):
fields = str.split(content, ',')
insert_row_countries(cursor, 'countries', fields)
content = fd.readline()
def process_file_verda (cursor, conn):
fd = open( "/home/larsfp/git/pyyrlib/verda2.txt" )
#content = fd.readline() #header
content = fd.readline()
while (content != "" ):
fields = str.split(content, ',')
insert_row_verda(cursor, conn, 'verda', fields)
content = fd.readline()
def all_lower (str):
return str.strip().lower().replace('Æ', 'æ').replace('Ø', 'ø').replace('Å', 'å')
conn, c = get_db_cursor ()
clear_db (c, 'countries')
process_file_countries (c)
clear_db (c, 'verda')
process_file_verda(c, conn)