我是第一次使用MySQL连接进行python编程。 我正在从事12年级的项目 在股票市场或主要市场又称为新发行市场上。 这是我项目的购买模块。
如果我分开运行此代码,它将运行无错误,但将其合并为所有 发生此错误。
--'int'和'list'的不受支持的操作数类型
我不知道为什么会发生
def converl(results,l):
#convert tuple into list
for x in results:
for z in x:
l.append(z)
return(l)
import mysql.connector as mycon
mydb = mycon.connect(user = 'root',
password = '',
host = 'localhost',
database = 'stockmarket')
cname =input('enter name of company to purchase shares ')
uname = 'amit'
sql = "select cname from companies"
cursor = mydb.cursor()
cursor.execute(sql)
results = cursor.fetchall()
l=[]
t = converl(results,l)
if cname in l:
try:
csql = "select (purchased_shares) from purchase where cname = '%s'"%(cname)
cursor = mydb.cursor()
cursor.execute(csql)
cresult = cursor.fetchall()
s = len(cresult)
res = []
for i in cresult:
t_sum = 0
for j in i:
t_sum += j
res.append(t_sum)
t = 0
for z in range(0,s):
t = t + res[z]
zsql = "select (issued_shares) from companies where cname = '%s'"%(cname)
cursor.execute(zsql)
zresult = cursor.fetchone()
y = zresult[0]
#finding shares left
share_left = y - t
print(share_left)
if share_left>0:
print(share_left,'available shares')
no_of_shares = int(input('enter no of shares to be purchased '))
if no_of_shares>zresult[0]:
print('purchase shares should be equal to or less than shares left')
else:
csql = "select cno from companies where cname ='%s'"%(cname)
cursor.execute(csql)
cresult = cursor.fetchone()
psql = "select rate from companies where cname ='%s'"%(cname)
cursor.execute(psql)
presult = cursor.fetchone()
#find total_paid
total_paid = presult[0] * no_of_shares
print(total_paid)
#Insert into purchase
sql = "insert into purchase (cno,username,cname,purchased_shares,total_paid) values('%d','%s','%s','%d','%d')"%(cresult[0],uname,cname,no_of_shares,total_paid)
cursor = mydb.cursor()
cursor.execute(sql)
#update companies table
comsql = "update companies set sub_shares = sub_shares + '%d' where cname ='%s'"%(no_of_shares,cname)
cursor = mydb.cursor()
cursor.execute(comsql)
mydb.commit()
print(no_of_shares,'Shares purchased')
lnmenu()
#Insert into purchase
else:
print('No more shares left')
tryagain()
except Exception as expt:
print(expt)
else:
print('company not found')
tryagain()
答案 0 :(得分:1)
最后,我解决了
只需更改此
s = len(cresult)
res = []
for i in cresult:
t_sum = 0
for j in i:
t_sum += j
res.append(t_sum)
t = sum(res)