[英]Get list without JSON-like style mysql python
I am building a POS (point of sale) in python with MySQL! 我正在使用MySQL在python中构建POS(销售点)! I want to list all articles, but I get the JSON-like output.
我想列出所有文章,但得到类似JSON的输出。 My code:
我的代码:
import mysql.connector print('eXperience POS\\nArtikli:') posdb = mysql.connector.connect\\ ( user='root', password='', host='127.0.0.1', database='experiencepos' ) try: cursor = posdb.cursor() cursor.execute('SELECT * FROM artikli') sviartikli = cursor.fetchall() print(sviartikli) finally: posdb.close()
I think you're confusing JSON output with it printing a dict
. 我认为您在将JSON输出与输出
dict
混淆。 Each item in the cursor returns a dict
containing the item's fields. 光标中的每个项目都会返回一个包含该项目字段的
dict
。 Since you did not show the example output I'll just use a name and description field for example. 由于您没有显示示例输出,因此仅使用名称和描述字段作为示例。 This is what I think you're looking for though.
这是我认为您正在寻找的东西。 Also you may want to just loop through the cursor as printing the entire cursor would just print a
list
of dicts
with all the items. 另外,您可能只想遍历光标,因为打印整个光标只会打印包含所有项目的
dicts
list
。
import mysql.connector
print('eXperience POS\nArtikli:')
posdb = mysql.connector.connect\
(
user='root',
password='',
host='127.0.0.1',
database='experiencepos'
)
try:
cursor = posdb.cursor()
cursor.execute('SELECT * FROM artikli')
for sviartikli in cursor:
name = sviartikli['name'] #example field
description = sviartikli['description'] #another example field
print("Name: {} Description: {}")
finally:
posdb.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.