Use the sqlalchemy text function to remove the interpretation of special characters:
Note the use of the text("your_insert_statement") function text("your_insert_statement") below. What he does tells sqlalchemy that all question marks and percent signs in the passed string should be treated as literals.
import sqlalchemy from sqlalchemy import text from sqlalchemy.orm import sessionmaker from datetime import datetime import re engine = sqlalchemy.create_engine("mysql+mysqlconnector://%s:%s@%s/%s" % ("your_username", "your_password", "your_hostname_mysql_server:3306", "your_database"), pool_size=3, pool_recycle=3600) conn = engine.connect() myfile = open('access2.log', 'r') lines = myfile.readlines() penguins = [] for line in lines: elements = re.split('\s+', line) print "item: " + elements[0] linedate = datetime.fromtimestamp(float(elements[0])) mydate = linedate.strftime("%Y-%m-%d %H:%M:%S.%f") penguins.append(text( "insert into your_table (foobar) values('%%%????')")) for penguin in penguins: print penguin conn.execute(penguin) conn.close()
Eric Leschinski Nov 19 '14 at 23:23 2014-11-19 23:23
source share