我正在尝试从数据库中获取值。当我打印时;我得到以下序列
- ('Total Row(s):', 4)
- [202]
- [226]
- [561]
- [23]
但我需要他们转换为list = [202,226,561,23]
等列表,但我无法得到这个。所有元素都显示在第0位。
有人可以帮帮我吗?
这是我的代码,我正在连接mysql,我正在连接mysql:
import mysql.connector
from mysql.connector import MySQLConnection, Error
conn = mysql.connector.connect(host='examplecom',
database='niya')
cursor = conn.cursor()
old_Value = raw_input("Please enter old Pincode: ")
print "Pincode is entered by you is : %s" % old_Value
query_Value = ("select `id`,`type_id`,"
"mappings`.`facility_id`,"
"mappings`.`service_area_id` from `service_areas`,"
"`mappings` where mappings`.`service_area_id`=`id` "
"and `type_id`='{0}'".format(old_Value))
cursor.execute(query_Value, )
rows = cursor.fetchall()
print('Total Row(s):', cursor.rowcount)
for row in rows:
list_Id = [];
facility_Id = int(row[2])
list_Id.append(facility_Id)
print list_Id
答案 0 :(得分:0)
改变这个:
for row in rows:
list_Id = [];
facility_Id = int(row[2])
list_Id.append(facility_Id)
print list_Id
成:
list_Id = []
for row in rows:
facility_Id = int(row[2])
list_Id.append(facility_Id)
print list_Id
您为每一行创建了一个新列表,但想要一个包含所有ID的列表,即所有行。