-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewNetNoche.py
78 lines (62 loc) · 2.41 KB
/
newNetNoche.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
import mysql.connector
import csv
# the user input data goes here:
filepath ='ir.csv'
#the tableName should follow this pattern : dbName.tableName
tableName='fortest.boz'
dbName= 'fortest'
def mysqlConnector():
""" Connect to MySQL database """
conn = None
try:
conn = mysql.connector.connect(
host='localhost',
user='karen',
password='Karen22111357*',
)
if conn.is_connected():
print('Connected to MySQL database')
# conn.escape_string()
return conn
except mysql.connector.Error as error:
print("parameterized query failed {}".format(error))
# an automated function with this output :
#'INSERT INTO ks.currencies (Country,CountryCode,Currency,Code) VALUES(%s,%s,%s,%s)'
def autoSqlProducer():
# INSERT INTO ISP.iranIsp (form , to , number , dateassigned , incName ) VALUES (%s,%s,%s,%s,%s)
# cursor.execute(sql,(row[0],row[1],row[2],row[3]))
with open('ir.csv','r+' ) as f:
csvR=csv.reader(f)
row1 =next(csvR)
L=range(len(row1))
# str for using in sql column specifier
cn=''
for i in L:
cn = cn + row1[i] +","
cn = "("+ cn[:-1] +")"
# str for using in sql value specifier
val =''
for i in L:
val=val +"%s,"
val ="("+val[:-1]+")"
sql =f"INSERT INTO {tableName} {cn} VALUES {val}"
return sql
# mc.execute(stmtInsertAgahi,(agahi['description'],agahi['price'],agahi['size'],agahi['location'],' '.join(agahi['phone']),j[0],agahi['sahebAD'],agahi['title'],agahi['city'],agahi['img']))
# stmtInsertAgahi='INSERT INTO Moozmar.agahi(description,price,size,location,phone,url,sahebAgahi,title,city,img)VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
def insertion():
with open(filepath,'r') as readObj:
csvReader=csv.reader(readObj)
# removing the headers : attention if you are using you must not remove the first row
# next(csvReader,None)
for row in csvReader:
# print(row)
sql=str(autoSqlProducer())
# # print(row[0],row[1],row[2],row[3],row[4])
cursor.execute(sql,(row[0],row[1],row[2],row[3],row[4]))
mydb.commit()
# running the program
mysqlConnector()
mydb=mysqlConnector()
cursor=mydb.cursor(prepared=True)
insertion()
cursor.close()