I have a problem inserting values to a MSSQL database. The string comes from a tuplified Pandas Dataframe and shall be inserted via sqlalchemy/pyodbc. Since the Panda Dataframe "to_sql" method is to slow I wanted to insert values in a batch of 1000 and built the following method (in accordance to a tutorial found on the web):
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
def mssql_insert_rows(records,db_extract, target_tbl):
insert_ = "INSERT INTO dbo."+ db_extract+ "_"+ target_tbl+ " VALUES "
for batch in chunker (records, 1000):
rows = ','.join(batch)
insert_rows = insert_+rows
cursorSQL.execute(insert_rows)
connSQL.commit()
This is how the string looks like:
(\'2\', \'10\', \'41\', \'null\', \'null\', \'null\', \'None\', \'FR\', "Saint-ouen l\'aumône ", \'2013_07M\', \'False\', \'True\', \'113706\', \'1990-01-01 00:00:00.0\', \'2013-04-30 00:00:00.0\', \'9AAE305417\', \'null\', \'None\', \'False\', \'False\')
As cou can see the value "Saint-ouen l\'aumône " is quite bad for inserting since the program now thinks it's the name of a column. I tried different approaches like df.replace or str.replace to solve this problem but nothing really gets me to a solution.
Does anyone here know a good and easy way to convert values like this one? I personally wouldn't mind if the value looks like
Saint-ouen laumone
or so.
Thanks in advance! :-)