Search code examples
pythonmysqlstringinsertmysql-error-1054

python, mysql, inserting string into table, error 1054


I am having the problem OperationalError: (1054, "Unknown column 'Ellie' in 'field list'") With the code below, I'm trying to insert data from json into a my sql database. The problem happens whenever I try to insert a string in this case "Ellie" This is something do to with string interpolation I think but I cant get it to work despite trying some other solutions I have seen here..

CREATE TABLE

con = MySQLdb.connect('localhost','root','','tweetsdb01')
cursor = con.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS User(user_id BIGINT NOT NULL PRIMARY KEY, username varchar(25) NOT NULL,user varchar(25) NOT NULL) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB")
con.commit() 

INSERT INTO

def populate_user(a,b,c):
    con = MySQLdb.connect('localhost','root','','tweetsdb01')
    cursor = con.cursor()
    cursor.execute("INSERT INTO User(user_id,username,user) VALUES(%s,%s,%s)"%(a,b,c))
    con.commit() 
    cursor.close() 

READ FILE- this calls the populate method above

def read(file):
    json_data=open(file)
    tweets = []
    for i in range(10):
        tweet = json.loads(json_data.readline())

    populate_user(tweet['from_user_id'],tweet['from_user_name'],tweet['from_user'])

Solution

  • Use parametrized SQL:

    cursor.execute("INSERT INTO User(user_id,username,user) VALUES (%s,%s,%s)", (a,b,c))
    

    (Notice the values (a,b,c) are passed to the function execute as a second argument, not as part of the first argument through string interpolation). MySQLdb will properly quote the arguments for you.


    PS. As Vajk Hermecz notes, the problem occurs because the string 'Ellie' is not being properly quoted.

    When you do the string interpolation with "(%s,)" % (a,) you get (Ellie,) whereas what you really want is ('Ellie',). But don't bother doing the quoting yourself. It is safer and easier to use parametrized SQL.