我创建了一个包含表TESTDB
的mysql数据库EMPLOYEE
,其中包含六列id
,FIRST_NAME
,LAST_NAME
,{{1 },AGE
和SEX
。我想要一个程序从用户那里获取输入,然后将数据存储到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'")