将MySql数据库值Pul到liststore Python3和Mysql.conn

2024-09-28 17:23:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

ValueError: row sequence has the incorrect number of elements

我的liststore对象是为单行gchararray配置的

我的代码如下:

^{pr2}$

如果我插入:

dblist = [['1'], ['2'], ['3']]

就在之前

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

它工作得很好。在


Tags: 数据对象数据库应用程序列表链接mysqllist
1条回答
网友
1楼 · 发布于 2024-09-28 17:23:24

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

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

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

或者只是

^{pr2}$

虽然你可以简单地

self.list_customers = cnx.fetchall()

如果gtx接口不坚持列表。。在

相关问题 更多 >