When I try to access the class instance using the name generated by a for loop I am getting
"AttributeError: 'str' object has no attribute".
I am new to Python and please excuse any "Not the best practice". Any help is appreciated
class Csv:
def __init__(self, invoicenum, customer, invoicedate, duedate, description, amount, productservice, invoiceclass):
self.invoicenum = invoicenum
self.customer = customer
self.invoicedate = invoicedate
self.duedate = duedate
self.description = description
self.amount = amount
self.productservice = productservice
self.invoiceclass = invoiceclass
self.line = '{},{},{},{},{},{},{},{}\n'.format(invoicenum, customer, invoicedate,
duedate, description, amount, productservice, invoiceclass)
def wite_to_db(self):
mydb = mysql.connector.connect(host="localhost", user="root",
passwd="passwd", database="invoice")
mycursor = mydb.cursor()
sql = "insert into invoice " "(Invoice_no, Customer, Invoice_Date, Due_date, Description, Amount, Product_service, Class)" "VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
val = (self.invoicenum, self.customer, self.invoicedate, self.duedate,
self.description, self.amount, self.productservice, self.invoiceclass)
mycursor.execute(sql, val)
mydb.commit()
x0 = Csv("1355", "Test1 User", "2020-05-03", "2020-12-31", 7095, 300, "Tithe", "General Fund")
x1 = Csv("1124", "Test2 User", "2020-05-03", "2020-12-31", 7095, 300, "Tithe", "General Fund")
x2 = Csv("14432", "Test3 User", "2020-05-03", "2020-12-31", 7095, 300, "Tithe", "General Fund")
x3 = Csv("1312", "Test4 User", "2020-05-03", "2020-12-31", 7095, 300, "Tithe", "General Fund")
line_range = range(4)
for line in line_range:
line_instance = 'x' + str(line)
print(line_instance)
line_instance.write_to_db()
line_instance is a str. str has no function called write_to_db