I have MySQL (5.6.10) table with two columns A: varchar(255), B: text
When I run the following:
from mysql import connector as sqlserver
Database = sqlserver.connect(host=host, user=user, passwd=passwd, db=db)
Database.autocommit = True
Cursor = Database.cursor()
Cursor.execute('INSERT INTO Table (A, B) VALUES (%s, %s)',
tuple(['This_is_a_test', '\n\nThis is a test\n\n']))
Raises the exception:
Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash
If I do instead:
import json
Cursor.execute('INSERT INTO Table (A, B) VALUES (%s, %s)',
tuple(['This_is_a_test', json.dumps('\n\nThis is a test\n\n')]))
Then it is successful, however the value in the table is:
'"\n\nThis is a test\n\n"'
Instead of:
'
This is a test
'
I can enter in the value manually to have the correct format, but how should you pass in the string as a variable and have it keep the correct formatting?