Python 3 多维列表由现有 MySQL table 填充

Python 3 Multidimentional List populated by existing MySQL table

我有一个现有的 MySQL 数据库,需要在不重写代码的情况下更改行。我引用了 stackexchange Two dimensional array in python。尝试了这些更正(示例可能被注释掉了)。所有登录信息已更改。

import mysql.connector # Import MySQL Connector
import os 
from sys import argv   # Allow Arguments to be passed to this script

# DEFINE Global Variables
dataVar=[[],[]]  # looked on stackexchange - failed
#dataVar=[]         # Originally Did this - failed
#dataVar.append([]) # And This
#for i in range(20): dataVar.append(i) # Added this - failed

# MySQL Column Names -- Python doesn't allow string index of Lists.

UUID=1;         # Unique Unit IDentifier
Name=10;
Type=3;
Description=11; # Written Description
V2=18;
ImgUrl=15;      # Custom Icon
InStock=4;      # Is it in stock 'Yes' or 'No'
New=2;          # Display New Placard 'Yes' or 'No'
Damage=5;       # Display Damage Warning 'Yes' or 'No'
Tankable=12;    # Display Tank Warning 'Yes' or 'No'
Display=19;     # Display If in stock 'Yes' or 'No'
Mix=16;         # How to mix
Customer=17;    # Customer Name
Profile=13;     # Searchable keywords not in Description
ml005=14;       # Prices u=Unavailble s=Standard
ml015=6;        # Prices u=Unavailble s=Standard
ml030=7;        # Prices u=Unavailble s=Standard
ml120=8;        # Prices u=Unavailble s=Standard
Shots=9;        # Prices u=Unavailble s=Standard


price005='.50';
price015='.00';
price030='.00'; 
price120='u';      # No 120ml Bottles


def fetchData():{
global totalCount # totalCount able to be written to in this function
global dataVar      # Data able to be written to in this function
MySQLcon=mysql.connector.connect(user='u', password='p', host='localhost', database='d')
if (MySQLcon):
cursor=MySQLcon.cursor()
query="SELECT * FROM eJuice  ORDER BY Name";
cursor.execute(query)
results=cursor.fetchall()
MySQLcon.close
totalCount=0;
for row in results: {         # Fetch eJuice data
dataVar[UUID].append(row[0]);
dataVar[New].append(row[1]);
dataVar[Type].append(row[2]);
dataVar[InStock].append(row[3]);
dataVar[Damage].append(row[4]);
dataVar[ml015].append(row[5]);
dataVar[ml030].append(row[6]);
dataVar[ml120].append(row[7]);
dataVar[Shots].append(row[8]);
dataVar[Name].append(row[9]);
dataVar[Description].append(row[10]);
dataVar[Tankable].append(row[11]);
dataVar[Profile].append(row[12]);
dataVar[ml005].append(row[13]);
dataVar[ImgUrl].append(row[14]);
dataVar[Mix].append(row[15]);
dataVar[Customer].append(row[16]);
dataVar[V2].append(row[17]);
dataVar[Display].append(row[18]);
totalCount+=1;
}# End for row in results
}# End with MySQLcon
return
}# End fetchData()

# Start Program

fetchData();
# Create Display Function
# End Program

来自 运行 以上代码的 CLI 输出: $ python3 main.py
Traceback (most recent call last):
File "main.py", line 88, in <module>
fetchData(); # Used from MySQL.py
File "main.py", line 61, in fetchData
dataVar[UUID].append(row[0]);
AttributeError: 'int' object has no attribute 'append'

您需要定义一个列表,其中的列表数与数据中的列数一样多-table。你可以这样做:

unitUUID = []
unitNew = []
unitType = []
...
dataVar[unitUUID, unitNew, unitType,...]

您现在可以继续将项目附加到每个列表:

unitUUID.append(row[0])

等等。请注意,从 Two dimensional array in Python 的解释中可以清楚地看出这一点。我建议你仔细阅读那篇文章。