英文:
Insert data in SQL Server with user input using loops in python
问题
我是你的中文翻译,以下是翻译好的部分:
"我是 Python 的新手,我想将用户输入和计算的数据插入到 SQL 表中。以下是我目前的代码:
active = list(c.execute("SELECT hrm_id, name, wage FROM employees WHERE active=1;"))
payroll_date = input("请输入工资单日期:")
for i in range(len(active)):
print(active[1])
days_worked = float(input("请输入工作天数:"))
wage = float(active[2])
hrm_id = int(active[0])
amount_owed = days_worked * wage
sql = "INSERT INTO dbo.payroll(hrm_id, days_worked, amt_owed, payroll_date) VALUES(?, ?, ?, ?)"
data = [hrm_id, days_worked, amount_owed, payroll_date]
c.execute(sql, data)
c.commit()
这是一个基于控制台的程序,我想要打印姓名,输入工作天数,然后计算工资,但是我遇到了以下错误:
Traceback (most recent call last):
File "c:\Users\gnsta\OneDrive\Payroll App\payrollcalc.py", line 14, in <module>
wage = float(active[2])
TypeError: float() argument must be a string or a real number, not 'pyodbc.Row'
我明白数据不是浮点数和字符串,而是 pyodbc.Row,但我找不到改变它的方法。"
英文:
I'm new to python and I want to insert data from user input and calculations into an SQL table. Here is the code I have so far:
active = list(c.execute("SELECT hrm_id, name, wage FROM employees WHERE active=1;"))
payroll_date = input("Add the date of the payroll: ")
for i in range(len(active)):
print(active[1])
days_worked = float(input("Type days worked: "))
wage = float(active[2])
hrm_id = int(active[0])
amount_owed = days_worked * wage
sql = "INSERT INTO dbo.payroll(hrm_id, days_worked, amt_owed, payroll_date) VALUES(?, ?, ?, ?)"
data = [hrm_id, days_worked, amount_owed, payroll_date]
c.execute(sql, data)
c.commit()
It is console based and I want to print the name, type the days and then calculate the salary, but I get this error:
Traceback (most recent call last):
File "c:\Users\gnsta\OneDrive\Payroll App\payrollcalc.py", line 14, in <module>
wage = float(active[2])
TypeError: float() argument must be a string or a real number, not 'pyodbc.Row'
I get that the data isn't float and strings but pyodbc.Row, but I can't find a way to change that.
答案1
得分: 0
Call fetchall()
and iterate over the rows directly rather than trying to use len()
:
cursor = conn.execute("....")
for row in cursor.fetchall():
print(row[1]) // etc
Read the documentation for more details.
英文:
Call fetchall()
and iterate over the rows directly rather than trying to use len()
:
cursor = conn.execute("....")
for row in cursor.fetchall():
print(row[1]) // etc
Read the docuementation for more details.
答案2
得分: 0
"Logs Logs Logs tells you the exact problem. active[2] is returning entire row of data instead of wage value. Always focus on logs it tells you all how to fix it i.e. TypeError: float() argument must be a string or a real number, not 'pyodbc.Row'"
"It is returning row instead. Now you got it right! I will not write the code but I am sure you can fix it. Now focus on type error and you will get to the solution. hint, for emp active:"
"wage = float(employee[2])"
"hrm_id = int(employee[0])"
"Now you got it!"
英文:
Logs Logs Logs tells you the exact problem. active[2] is returning entire row of data instead of wage value. Always focus on logs it tells you all how to fix it i.e. TypeError: float() argument must be a string or a real number, not 'pyodbc.Row'
It is returning row instead. Now you got it right! I will not write the code but I am sure you can fix it. Now focus on type error and you will get to the solution. hint,
for emp active:
wage = float(employee[2])
hrm_id = int(employee[0])
Now you got it!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论