TypeError: argument 1 must be a string or unicode object: got tuple instead

I want to insert my data to postgresql with psycopg2.

My addRecord function like that:

def addRecord(self, val1, val2, val3, val4, val5, val6, val7, val8):
        query = """
        INSERT INTO stories (
        val1, 
        val2, 
        val3, 
        val4, 
        val5, 
        val6, 
        val7, 
        val8) 
        VALUES(%s, %s, %s, %s, %s, %s,%s, %s)""", (val1, val2, val3, val4,val5, val6,val7,val8)
        self.cursor.execute(query)
        self.connection.commit()

and I call this definition like that:

db.addRecord("asdasd", "asfasf", "asfasfa", "afasfwqe", "afqwrqwr", "dferqwrqw", "afasf", "afasfasf")

But when I try to insert this data I get this error:

TypeError: argument 1 must be a string or unicode object: got tuple instead

Why Im getting this error. How can I solve this?

Upvotes: 2

Views: 3307

Answers (1)

DeepSpace
DeepSpace

Reputation: 81684

query is a tuple of the query and the arguments. Instead, you should pass the arguments to the execute method:

    query = """
    INSERT INTO stories (
    val1, 
    val2, 
    val3, 
    val4, 
    val5, 
    val6, 
    val7, 
    val8) 
    VALUES(%s, %s, %s, %s, %s, %s,%s, %s)"""
    self.cursor.execute(query, (val1, val2, val3, val4,val5, val6,val7,val8))

Upvotes: 5

Related Questions