single backslash needed in SQL Server query from Python / SQLAlchemy -
i trying write query using bulk insert
function in sql server 2014 via python script insert large csv file table in database. have following example code:
import pymssql import sqlalchemy engine = create_engine('connection_string_goes_here', encoding="utf-8") table_name = 'my_table' source_path = 'c:\path\to\csv\test.csv' q = ("bulk insert " + table_name + " " + "from '" + source_path + "' " + "with ( datafiletype = 'char', " + "fieldterminator = ',', " + "rowterminator = '\\n')") connection = engine.connect() connection.execute(q) connection.close()
the error is:
(pymssql.operationalerror) (4861, cannot bulk load because file "c:\\path\\to\\csv\\test.csv" not opened. operating system error code 3(the system cannot find path specified.).db-lib error message 20018…
i believe because of double backslash issue. backslashes being literally included in bulk insert statement instead of being translated single backslashes. don't see in sql server error log.
my setup uses ms sql server express 2014 , python 2.7.9 running on separate windows box.
i have tried using raw strings (r'c:\path\to\csv\test.csv
) , decode
string function on source_path
, neither have helped.
looks using /
instead of \
works.
Comments
Post a Comment