0

I have an existing MySQL database that needs to change in rows with out rewriting code. I have referenced stackexchange Two dimensional array in python. Attempted those corrections (Examples maybe commented out). All login information HAS been changed.

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='$2.50';
price015='$5.00';
price030='$10.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 output from running above code: $ 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'

4

1 回答 1

0

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

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

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

unitUUID.append(row[0])

等等。请注意,这从Python 中二维数组的解释中非常清楚。我建议你仔细阅读那篇文章。

于 2015-09-15T18:28:46.303 回答