【发布时间】:2018-07-31 10:23:10
【问题描述】:
我有一个 MySQL 表,其中有许多行,我不知道。 我设法使用这个函数显示了前 3 行的内容:
def read_some_data():
read_query_bis = """SELECT * FROM """ + table_name + " ;"
cursor.execute(read_query_bis)
rows = cursor.fetchall()
print("*** DEBUG FUNCTION Read",cursor.rowcount,"row(s) of data.")
# Print first 3 columns of all rows
for row in rows:
print("*** DEBUG FUNCTION Data row = (%s, %s, %s)" %(str(row[0]), str(row[1]), str(row[2])))
列数未知,有没有办法使用 fetchall 和循环来获取所有行和所有列而不是给定数字(在我的示例中,所有行为 3)?
编辑:至于下面的 Remarque,我可以添加类似的内容:
Rows_var_placeholders = ", ".join(["%s"] * Rows_Lengh)
哪朵云给了我:
%s, %s, %s, %s, %s, %s
我可以使用,但我的问题更多的是“str(row[0]”
【问题讨论】:
-
试试
len(row)? -
是的,我虽然是这样,但我确实想做循环,比如:“for i in len(row) print”,因为它会按顺序显示,我不想要.
-
我可以做一些事情 "Rows_var_placeholders = ", ".join(["%s"] * Rows_Lengh)" 得到 (%s, %s, %s, %s, %s, % s) 但我应该如何处理“str(row[i])”?