我正在尝试将值保存到数据库中,但是当尝试输入数据时,该值未出现在表中,并且显示了除外的输出。
connection = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = 'pswrd'
)
mycursor = connection.cursor()
mycursor.execute('USE test')
def save_results():
print('Please enter your first and last name...')
name = (input('Enter: '))
try:
mycursor.execute('USE test')
mycursor.execute("SELECT UserID FROM users WHERE (name) = ('" + name +"');")
mycursor.execute("INSERT INTO users (name) VALUES ('" + name + "');")
connection.commit()
print('s')
except:
print('ASDASD')
我希望输入内容出现在表格中。
答案 0 :(得分:0)
这听起来完全是愚蠢的,但是,您检查密码是否正确?
答案 1 :(得分:0)
在建立连接对象时,您省略了定义数据库名称的操作:
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword",
database="mydatabase"
)