Im trying to pass a string varible to an sql query but I get the following error got tuple instead even though I'm passing a string.
& When I use the following command
command = ("INSERT INTO dev.roles (name) VALUES ('student')")it works fine... any feedback is appreciated
import psycopg2
import pandas as pd
con=psycopg2.connect(
dbname= 'xxxxx',
host='xxxxxxxxxxxxxxxxxxxx',
port= 'xxxx',
user= 'xxxxx',
password= 'xxxxxxxxx',
)
cur = con.cursor()
mystr="student"
print("Varible Type: ",type(mystr)) # output: Varible Type: <class 'str'>
command = ("INSERT INTO dev.roles (name) VALUES (%s)",(mystr))
cur.execute(command)
con.commit()
cur.close()
con.close()
This code outputs the following
Varible Type: <class 'str'>
Traceback (most recent call last):
File "myFile.py", line 17, in <module>
cur.execute(command)
TypeError: argument 1 must be a string or unicode object: got tuple instead
mystr.