Reputation: 29
I am trying to print the value of a column from a pyodbc query using variables so i can do some dynamic stuff.
columnName = "test1"
query = "SELECT test1,test2,test3 FROM testTable"
pyodbc.cursor.execute(query)
row = pyodbc.cursor.fetchone()
print(row.columnName)
Upvotes: 0
Views: 1022
Reputation:
You can use a variable like the one below to get the values for that column:
from pandas import DataFrame
import pyodbc
columnName = "test1"
query = "SELECT {} FROM testTable".format(columnName)
cursor.execute(query)
DF = DataFrame(cursor.fetchall())
Also you can get the columns data:
rows = cursor.fetchall()
Which is returned as a data tuple.
It is better not to take your command parameters from the user or the outside space because you may get a SQL injection attack.
Cursor parameters are also used to set values
so you can use the above solution and is not used for this purpose.
You can access all attribute of cursor in : https://github.com/mkleehammer/pyodbc/wiki/Cursor#attributes
Upvotes: 0