import pymysql
conn1 = pymysql.connect(host="127.0.0.1",user="root",password="1234",db="mypydb")
x = 'firsttitle'
y = 'firstkeyword'
sql = "INSERT INTO mytb(title,keywd) VALUES(%s,%s)"%(x,y)
conn1.query(sql)
conn1.commit()
我得到的错误是:
File "/home/feng/PycharmProjects/dangdang/mycwpjt/mycwpjt/spiders/my_sql.py", line 15, in
conn1.query(sql)
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 860, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 1061, in _read_query_result
result.read()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 1349, in read
first_packet = self.connection._read_packet()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 1018, in _read_packet
packet.check_error()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 384, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python3.5/dist-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1054, "Unknown column 'firsttitle' in 'field list'")
解决方案:
把这一行代码
sql = "INSERT INTO mytb(title,keywd) VALUES(%s,%s)"%(x,y)
改为:
sql = "INSERT INTO mytb(title,keywd) VALUES('%s','%s')"%(x,y)