Reputation: 13
I'm trying to parse a value from argument using argparse. Which works fine when I test my conditionals with "print". The error happens when I now add the values to my sql code. Pardon my not so pretty sql query, it's the only way I could get the data that I need.
Error message: TypeError: must be string or read-only buffer, not tuple
import mysqldb
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-m','--machine', nargs=1, help="Machine Hostname", action="store")
args = parser.parse_args()
query = (""" select ifname, usname, mtdesc, lobuilding Location from
interface left join machine on ifmcid=mcid
left join machine_type on mcmtid=mtid left join user on mcusid=usid left join location on mcloid=loid
WHERE mccurrentosid = 32 AND ifname = %s """, (args.machine,))
cur.execute(query )
for row in cur.fetchall():
print row
Upvotes: 0
Views: 1245
Reputation: 13
After trying out different permutations, finally got a working code. I had to remove the ',' in args.machine. Maybe the 'left join' statements doesn't need it anymore.
cur.execute(query, (args.machine))
Thanks for the help @alecxe
Upvotes: 0
Reputation: 473863
The query parameters should be passed into execute()
:
query = """
select
ifname, usname, mtdesc, lobuilding
from
interface
left join
machine
on
ifmcid=mcid
left join
machine_type
on
mcmtid=mtid
left join
user
on
mcusid=usid
left join
location
on
mcloid=loid
WHERE
mccurrentosid = 32 AND
ifname = %s
"""
cur.execute(query, (args.machine,) )
Upvotes: 1