This repository was archived by the owner on May 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertDatasetsToSqlStatements.py
More file actions
57 lines (53 loc) · 2.08 KB
/
Copy pathconvertDatasetsToSqlStatements.py
File metadata and controls
57 lines (53 loc) · 2.08 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
def convertDatasetsToInsertIntoStatements(datasetFilename,sqlTableName=None):
individualRecords = []
temp = []
finalList = []
columnDataTypes = []
with open(datasetFilename,mode='r') as datasetCSV:
datasetCSV.seek(3)
for row in datasetCSV:
columnNames = (row.strip().split(","))
break
for row in datasetCSV:
individualRecords.append(row.strip('\n').split(","))
for columnName in columnNames:
for char in columnName:
if char.startswith("("):
columnDataTypes.append(columnName[columnName.index(char)+1:columnName.index(columnName[-1])])
count = 0
for data in individualRecords:
for subData in data:
dataType = columnDataTypes[count]
if (
dataType in ("int", "decimal")
and subData == ''
or dataType not in ("int", "decimal")
and dataType in ("char", "varchar", "date")
and subData == ''
):
temp.append('NULL')
elif dataType in ("int", "decimal"):
temp.append(subData)
elif dataType in ("char", "varchar", "date"):
temp.append(f"'{subData}'")
count += 1
if count == len(columnDataTypes):
finalList.append(temp)
temp = []
count = 0
break
continue
with open("pasteTheseStatementsInSqlServer.txt", mode="w") as f:
f.write(f'INSERT INTO {sqlTableName.upper()}\n')
f.write('VALUES\n')
for data in finalList:
f.write('({}),\n'.format(",".join(data)))
if data == finalList[-1]:
f.write('({});\nGO'.format(",".join(data)))
'''
Just edit this part for your own usage
'''
convertDatasetsToInsertIntoStatements(
'propertyDataset.csv', #Replace this with the name of the excel file that you downloaded in CSV format
'property' #Replace this with the name of your table
)