Skip to content Skip to sidebar Skip to footer

Python Database Insert

I've been trying to parse a text file (opened with parameter encoding='utf8') and insert extracted values into an mdb database using pyodbc module. I have tried the code below: for

Solution 1:

You don't put quotes around the strings which you want to insert. Assuming the "Freq" row is of type INTEGER:

stmt = """
INSERT INTO Entries (PForm, WForm, Code, Freq, Pattern)
    VALUES ('%s', '%s', '%s', %s, '%s')
"""

params = tuple(t for t in tokens)

conn.execute(stmt % params)

But anyway, you shouldn't be formatting an INSERT statement like this. Doesn't the library you're using provide a facility to parameterize statements ? Something like this:

conn.execute("INSERT INTO Foo VALUES (?, ?, ?)", (foo, bar, baz))

Post a Comment for "Python Database Insert"