I’m using python scripts to send hundreds of thousands of insert queries to an sql server belonging to another system.
An example of my inserts:
"INSERT INTO ImportData (ISBNORISSNORURL, FILENAME, PDFLOCATIONS, PROCESSED) VALUES('0442019521', 'pinto_1995_245_hires.pdf', 'I:\\Collect and Process Data\\Licensing Schemes Processing\\03 Data Preparation Original\\USQ\\September-October 2010\\pinto_1995_245_hires.pdf','0');"
The file path string is assigned to a global:
PDFLOCATIONS = "I:\Collect and Process Data\Licensing Schemes Processing\03 Data Preparation Original\USQ\September-October 2010"
And this is then added to a dictionary consisting of name/value pairs which match up with the field and value data in the insert query. These are converted to strings of data:
final_header_list = str(fields_value_list.keys()).replace("[","").replace("]","").replace("'","")
final_values_list = str(fields_value_list.values()).replace("[","").replace("]","").replace("'","")
Which are then added to the final insert string:
insert_query = 'INSERT INTO {0} ({1}) VALUES({2});'.format(TABLE,final_header_list,final_value_list)
Which is sent to sql server in groups of 10000.
The double backslash goes through to the database as a double backslash rather than magically converting to a single backslash. I’m apparently not allowed to use a forward slash as an alternative. Is there any alternative method of getting the desired single backslash on my end? Or will the administrators of the SQL server instance have to make the change on their end?
Many thanks!
Try changing
to
(notice the raw –
r– indicator)EDIT: Trying again
Use an ugly beast approach:
Thus forcing the string to have no double backslashes. Of course I’d first try replacing
with
just to be completely sure that ugly approach would not be needed.