How do i execute select statement for all the 4 id's that is 10, 20, 34 and 1 and would expect the result to be in a list
import pymysql
import json
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='',
db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
cursor = conn.cursor()
select_query = "select * from users where id = "
ids = ["10", "20", "34", "1"]
for x in ids:
var = select_query + x
# print(var)
print(list(cursor.execute(var)))
asked Sep 19, 2017 at 16:09
deepak murthy
4113 gold badges6 silver badges21 bronze badges
-
What results are you currently getting? How are the desired results different from that?Eric Ed Lohmar– Eric Ed Lohmar2017年09月19日 16:23:41 +00:00Commented Sep 19, 2017 at 16:23
-
getting error - TypeError: 'int' object is not iterable. not sure where i went wrongdeepak murthy– deepak murthy2017年09月19日 16:25:39 +00:00Commented Sep 19, 2017 at 16:25
-
The stack trace will tell you the line at which the error occurred. Which line was it, in this case?Eric Ed Lohmar– Eric Ed Lohmar2017年09月19日 16:30:09 +00:00Commented Sep 19, 2017 at 16:30
-
stack trace - Traceback (most recent call last): File "C:\xampp\htdocs\sandbox\for.py", line 13, in <module> print(list(cursor.execute(var))) TypeError: 'int' object is not iterabledeepak murthy– deepak murthy2017年09月19日 16:32:55 +00:00Commented Sep 19, 2017 at 16:32
2 Answers 2
According to the PyMySQL docs, the cursor.execute() function returns an int, which is why your error is showing up. You'll want to call that, then use one of the fetch functions to actually return the results
import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='',
db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
cursor = conn.cursor()
select_query = "select * from users where id = "
ids = ["10", "20", "34", "1"]
for x in ids:
var = select_query + x
# print(var)
cursor.execute(var)
print(list(cursor.fetchall()))
or, if you want all of the results in one list:
import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='',
db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
cursor = conn.cursor()
lst = []
select_query = "select * from users where id = "
ids = ["10", "20", "34", "1"]
for x in ids:
var = select_query + x
# print(var)
cursor.execute(var)
lst.extend(cursor.fetchall())
print(lst)
or as a dictionary
import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='',
db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
cursor = conn.cursor()
results = {}
select_query = "select * from users where id = "
ids = ["10", "20", "34", "1"]
for x in ids:
var = select_query + x
# print(var)
cursor.execute(var)
results[x] = list(cursor.fetchall())
print(results)
answered Sep 19, 2017 at 16:38
Eric Ed Lohmar
1,9221 gold badge17 silver badges26 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You probably forgot to typecast x to str
import pymysql
import json
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='',
db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
cursor = conn.cursor()
select_query = "select * from users where id = "
ids = ["10", "20", "34", "1"]
for x in ids:
var = select_query + str(x) # here
# print(var)
print(list(cursor.execute(var)))
1 Comment
deepak murthy
getting same error - stack trace. Traceback (most recent call last): File "C:\xampp\htdocs\sandbox\for.py", line 14, in <module> print(list(cursor.execute(var))) TypeError: 'int' object is not iterable
lang-py