-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMySQL_import.py
More file actions
165 lines (140 loc) · 6.86 KB
/
Copy pathMySQL_import.py
File metadata and controls
165 lines (140 loc) · 6.86 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import csv
import os.path
import pymysql
# Record commit frequency.
commit_frequency = 25
# MySQL database login and location information.
print("Which MySQL database do you want to import your tsv or csv file into?")
while True:
username = input("\nEnter username: ").strip()
password = input("Enter password: ").strip()
hostname = input("Enter hostname (DNS name or IP address): ").strip()
port_num = input("Enter port number (default 3306): ").strip()
if port_num.isdigit():
port_num = int(port_num)
else:
print("Invalid port number: %s, starting over." % port_num)
continue
instance = input("Enter database/instance name: ").strip()
prompt = '\nUsername = "%s"\nPassword = "%s"\nHostname = "%s"\nPort Number = "%d"\nDatabase/instance = "%s"\n'
prompt += 'If this is not correct, press "return", otherwise enter any other character to accept: '
prompt = prompt % (username, password, hostname, port_num, instance)
response = input(prompt)
if response != "":
break
# Print properties of the pymysql library.
print(f"\npymysql library version: {pymysql.__version__}")
print(f"pymysql parameter style ('named', 'qmark', or 'pyformat'): {pymysql.paramstyle}")
# Make connection to database.
connection = pymysql.connect(user=username, password=password, host=hostname, port=port_num, database=instance)
# Create cursor.
cursor = connection.cursor()
# Prepare to get the name of the table to import into.
table_exists_select = (
"SELECT COUNT(table_name)\n"
"FROM information_schema.tables\n"
"WHERE table_type = 'BASE TABLE'\n"
"AND table_name = '%s'\n"
"AND table_schema = '%s'")
# Get the name of the table to import into.
while True:
print("\nWhich table do you want to import into? If it does not exist, this program creates a new table")
print("\nwith one varchar field for each column you import from your file.")
print("\nThese fields will have the same name as the column headers of the columns you import.")
table_name = input("Enter the table name: ").strip().upper()
# See if this table already exists.
sql = table_exists_select % (table_name, instance)
cursor.execute(sql)
rows = cursor.fetchall()
table_exists = (int(rows[0][0]) == 0)
# File to import.
while True:
filename = input("Enter full path of file to read: ").strip()
if os.path.exists(filename):
break
else:
print("%s does not exist, please try again." % filename)
# Get column delimiter.
# delimiter = ","
delimiter = input("Enter the delimiter used to separate columns (or 'tab' for tab): ").strip()
if delimiter.lower() == "tab":
delimiter = "\t"
# Is the first line in the file column-headers?
while True:
# line1_column_names = "Y"
line1_column_names = input("Does the first line have column names? (Y/N): ").strip()
if line1_column_names.upper() in ("Y", "N"):
line1_column_names = (line1_column_names.upper() == "Y")
break
else:
print("%s is an invalid choice, please try again." % line1_column_names)
with open(file=filename, encoding="utf8", newline='') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=delimiter)
for line_number, all_columns_list in enumerate(csv_reader):
for all_columns_list in csv_reader:
if line_number == 0:
# Make column headings uppercase.
for column_number, column_text in enumerate(all_columns_list):
all_columns_list[column_number] = column_text.upper()
# Print list of columns from which to choose to import.
prompt = "\nColumn Column\nNumber Header\n------ ----------"
for column_number, column_text in enumerate(all_columns_list):
prompt += "\n%6d %s" % (column_number, column_text)
prompt += "\n------ ----------"
prompt += "\nEnter a comma-separated list of the column numbers you want to import: "
# Choose columns to import.
chosen_col_num_list2 = input(prompt).split(',')
print()
chosen_col_num_list = []
for chosen_col_num in chosen_col_num_list2:
chosen_col_num = int(chosen_col_num.strip())
if chosen_col_num > len(all_columns_list) - 1:
print("%d is beyond the end of the list of columns!" % chosen_col_num)
exit(1)
else:
chosen_col_num_list.append(chosen_col_num)
# Remove duplicates from list.
chosen_col_num_list = list(set(chosen_col_num_list))
chosen_col_num_list.sort()
# Construct CREATE TABLE statement.
# table_name = filename.split('.')[0].upper()
primary_key_name = table_name + '_PKEY'
create_table_sql = "CREATE TABLE `%s` (" % table_name
create_table_sql += "\n`%s` int(11) NOT NULL AUTO_INCREMENT" % primary_key_name
for chosen_col_num in chosen_col_num_list:
create_table_sql += ",\n`%s` varchar(1024)" % all_columns_list[chosen_col_num]
create_table_sql += ',\nPRIMARY KEY(`%s`)' % primary_key_name
create_table_sql += '\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;'
print("\nAbout to create table %s with this SQL statement:\n\n%s\n" % (table_name, create_table_sql) )
# Execute CREATE TABLE statement.
cursor.execute(create_table_sql)
connection.commit()
# Construct list of column names to be inserted.
insert_columns_list = []
for chosen_col_num in chosen_col_num_list:
insert_columns_list.append(all_columns_list[chosen_col_num])
insert_columns = "( " + ", ".join(insert_columns_list) + " )"
# Construct bind variables expression.
insert_values_bind = ["%s"]*len(insert_columns_list)
insert_values_bind = "( " + ", ".join(insert_values_bind) + " )"
# Construct the SQL insert.
insert_sql = "INSERT INTO %s %s VALUES %s" % (table_name, insert_columns, insert_values_bind)
print("You will be inserting records using this SQL:\n\n%s\n" % insert_sql)
else:
# The remaining lines of the file contain column values.
# Construct list of values to be inserted.
insert_values_list = []
for chosen_col_num in chosen_col_num_list:
insert_values_list.append(all_columns_list[chosen_col_num])
insert_values_tuple = tuple(insert_values_list)
# Do the insert using bind variables.
cursor.execute(insert_sql, insert_values_tuple)
if line_number % commit_frequency == 0:
connection.commit()
print("Inserted %d records." % commit_frequency)
# Commit any remaining inserts.
connection.commit()
# Finish up.
cursor.close()
connection.close()
print("All Done.")