Python SQL select row from specific variable field -


i'd grab specific value row based on random variable. here's example table pid column "auto-increment primary key integer" , other 2 columns text

example-table

pid    name    phone ---    ----    ----- 1      bill    999-9999 2      joe     888-8888 

i'd throw random variable @ table

randomvariable = raw_input('enter something: ')

> 1

and have code return name

> bill

i know can use like...

randomvariable = raw_input('enter something: ') sql = ("select name example_table pid='%s'" % randomvariable) result = cursor.execute(sql) print result 

> bill

apparently using '%s' isn't secure , suggested use '?' in it's place.

randomvariable = raw_input('enter something: ') sql = ("select name example_table pid=?", randomvariable) result = cursor.execute(sql) print result 

but doesn't seem work me. end with...

"valueerror: operation parameter must str or unicode"

i realize grab rows , put them variable iterate on till find i'm looking i'm thinking wouldn't efficient large database. can point me in right direction this?

i believe you're meant use this

randomvariable = raw_input('enter something: ') sql = "select name example_table pid=?" result = cursor.execute(sql, randomvariable) print result 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -