如何通过从用户获取输入使用python将值插入到mysql中的表中

时间:2018-06-05 09:18:58

标签: python mysql

我创建了一个包含表TESTDB的mysql数据库EMPLOYEE,其中包含六列idFIRST_NAMELAST_NAME,{{1 },AGESEX。我想要一个程序从用户那里获取输入,然后将数据存储到INCOME表中。

EMPLOYEE

当我尝试执行此代码时,我收到错误:

import MySQLdb

db = MySQLdb.connect("localhost","testuser","test123","TESTDB")

cursor = db.cursor()

ID = int(raw_input("Enter ID: "))
fname = raw_input("Enter first name: ")
lname = raw_input("Enter second name: ")
age = int(raw_input("Enter age: "))
sex = raw_input("Enter gender: ")
income = int(raw_input("Enter income: "))

sql = "INSERT INTO EMPLOYEE(id, FIRST_NAME, LAST_NAME, AGE, SEX, 
INCOME)"
value = "VALUES (ID, fname, lname, age, sex, income)"

query = sql + value

cursor.execute(query)
db.commit()

db.close()

我也尝试过使用

Traceback (most recent call last):
  File "insert_op.py", line 22, in <module>
    cursor.execute(query)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 250, 
in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 
50, in defaulterrorhandler
    raise errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column 'fname' in 
'field list'") 

0 个答案:

没有答案