此脚本旨在充当命令行前端,以将记录添加到本地托管的 MySQL 数据库。
我收到此错误:
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'watermelon' in 'field list'
但是西瓜是我要输入的值,而不是列名!
这是脚本:
#! /usr/bin/python
#use command line as front end to enter new rows into locally hosted mysql database
import mysql.connector
#create inputs
new_fruit = raw_input('What fruit do you want to add? ')
new_fruit_type = raw_input('Which type of ' + new_fruit + '? ')
#connect to dbase
conn = mysql.connector.connect(user='root', password='xxxx', database='play')
#instansiate cursor
cursor = conn.cursor()
#define sql statement
add_record = "INSERT INTO fruit (name, variety) VALUES (%s, %s)" % (new_fruit, new_fruit_type)
#execute sql
cursor.execute(add_record)
#close out
conn.commit()
cursor.close()
conn.close()
和表模式:
mysql> describe fruit;
+---------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(30) | YES | | NULL | |
| variety | char(30) | YES | | NULL | |
+---------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)