Pul MySql数据库值到liststore-Python3和MySql.connector

Pul MySql数据库值到liststore-Python3和MySql.connector,python,mysql,python-3.x,Python,Mysql,Python 3.x,我对python还比较陌生,正在寻求一些帮助,以使它符合我的意愿。我正在开发一个小应用程序,它将使用数据库填充各种组合框,这些组合框都链接到各种liststore对象。我可以从MySQL中提取数据,但在尝试将值插入列表存储时,数据会崩溃。我要么最终: ValueError: row sequence has the incorrect number of elements 我的liststore对象是为单行gchararray配置的 我的代码如下所示: mysqldb = mysql.conn

我对python还比较陌生,正在寻求一些帮助,以使它符合我的意愿。我正在开发一个小应用程序,它将使用数据库填充各种组合框,这些组合框都链接到各种liststore对象。我可以从MySQL中提取数据,但在尝试将值插入列表存储时,数据会崩溃。我要么最终:

ValueError: row sequence has the incorrect number of elements
我的liststore对象是为单行gchararray配置的

我的代码如下所示:

mysqldb = mysql.connector.connect(**mysql_config)
cnx = mysqldb.cursor()
cnx.execute("SHOW TABLES")

for (table_name, ) in cnx:
    dblist.append(''.join(table_name))

for i in range(len(dblist)):
    self.list_customers.append(list(dblist[i]))

cell = Gtk.CellRendererText()
self.combo_systems.pack_start(cell, False)
self.combo_systems.set_active(0)
如果我插入以下内容,则作为健全性检查:

dblist = [['1'], ['2'], ['3']]
就在之前

for i in range(len(dbslit)):
    self.list_customers.append(list(dblist[i]))

它工作正常。

这将获得与
[['1']、['2']、['3']]相同的格式。

cnx = mysqldb.cursor()
cnx.execute("SHOW TABLES")

tables = cnx.fetchall()   #  [('table1',), ('table2',), ...]
self.list_customers = [list(rec) for rec in tables]
...
或者只是

self.list_customers = [list(rec) for rec in cnx.fetchall()]
虽然你可能会侥幸逃脱

self.list_customers = cnx.fetchall()

如果gtx界面不坚持列表列表,请在第一次
for
循环后打印
dblist
。它是否包含您所期望的内容?我应该添加for(table_name,)的输出。。。是('bar','foo','foobar')谢谢!!我无法直接为每个
self.list\u customers=[list(rec)for rec in tables]
分配值,但我可以将其分配给dblist,然后进行迭代和追加。非常感谢!在这种情况下,您可以尝试
self.list\u customers+=[list(rec)….