在Python中使用循环将用户输入的数据插入SQL Server。

huangapple go评论68阅读模式
英文:

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(&quot;SELECT hrm_id, name, wage FROM employees WHERE active=1;&quot;))

payroll_date = input(&quot;Add the date of the payroll: &quot;)

for i in range(len(active)):

print(active[1])
days_worked = float(input(&quot;Type days worked: &quot;))
wage = float(active[2])
hrm_id = int(active[0])
amount_owed = days_worked * wage

sql = &quot;INSERT INTO dbo.payroll(hrm_id, days_worked, amt_owed, payroll_date) VALUES(?, ?, ?, ?)&quot;
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 &quot;c:\Users\gnsta\OneDrive\Payroll App\payrollcalc.py&quot;, line 14, in &lt;module&gt;
    wage = float(active[2])
TypeError: float() argument must be a string or a real number, not &#39;pyodbc.Row&#39;

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(&quot;....&quot;)
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!

huangapple
  • 本文由 发表于 2023年5月7日 08:08:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定