创建mysql对象_从mysql数据库创建具有可变对象名的对象

我试图用变量名创建对象,当我打印出objectname变量时,会为其指定正确的名称。但是当我尝试使用objectname变量创建一个对象时,创建的对象实际上被称为“objectname”,而不是使用分配给变量的字符串。我的代码如下:class Customer:

# Initiliaise method, creating a customer object

def __init__(self,name):

self.name = name

print "Customer %s Added" % (self.name)

# Print out details

def output(self):

print "This is the customer object called %s" % (self.name)

## Create the Customer objects, from the Customer table

# Pull the Customers out of the Customer table

# SQL

cursor.execute("SELECT * FROM Customer")

result = cursor.fetchall()

for record in result:

objectname = 'Customer' + str(record[0])

print objectname # This prints "Customer1..2" etc

# customername is the exact name as in the database

customername = str(record[1])

# Use the above variables pulled from the database to create a customer object

objectname=Customer(customername)

# We need to count the number of customer objects we create

customercount = customercount + 1

因此,只需创建一个名为objectname的对象,而不是基于Customer DB表中的数字的多个对象“Customer1,2,3”等。变量名基于字符串“Customer”和数据库中的行ID。在

我假设我引用的变量不正确

谢谢你的帮助。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值