cmlakhan
cmlakhan

Reputation: 127

mysqldb error in python

I am trying to insert a value into my local MySQL instance using Python but I keep getting an error

Here is my code:

con = mdb.connect('localhost', 'root', 'password', 'testDB')
cur = con.cursor()
cur.execute('''INSERT INTO testDB.testPython VALUES (%s)''',("HelloWorld"))
con.commit()

but I am getting the following error

TypeError: not all arguments converted during string formatting

Does anyone know why?

Upvotes: 1

Views: 99

Answers (2)

Zulu
Zulu

Reputation: 9275

TypeError is a basic Python error:

Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch.

Your trouble is you forgot to place a comma in your tuple, and without it is considered as the object in parenthesis: your single string.

Explanation with interpreter:

In [1]: ('Hello')
Out[1]: 'Hello'
In [2]: ('Hello',)
Out[2]: ('Hello',)

Fix like this to resolve your trouble :

cur.execute('INSERT INTO testDB.testPython VALUES (%s)', ("HelloWorld",))

Upvotes: 3

harvey
harvey

Reputation: 2953

Don't use the triple quotes, this will treat your sql includ the parameter as a string literal. Use the standard quote ( " ) instead.

Upvotes: 0

Related Questions