因此,我和一个朋友正在尝试一起学习MySQLdb,我们从注册程序开始。现在是下面的代码:

import MySQLdb
import sys
import time

db = MySQLdb.connect(host="localhost",port=3306,user="root",passwd="",db="testing" )
cur = db.cursor()

def register():
    print "Welcome to user registration!"
    user = raw_input("Username: ")
    passw = raw_input("Password: ")
    confpass = raw_input("Confirm password: ")
    if confpass.lower() != passw.lower():
        print "Passwords do not match!"
        register()
    print "Almost done!"
    email = raw_input("E-mail: ")
    confemail = raw_input("Confirm E-mail: ")
    if email.lower() != confemail.lower():
        print "Emails do not match!"
        register()
    add_user = ("INSERT INTO testing1 "
                "username, password"
                "VALUES (%s, %s)")

    data_test = (user, passw)
    cur.execute(add_user, data_test)
    db.close()

choice = raw_input("Do you want to register or login?: ")
if choice.lower() == 'register':
    register()

db.close()


我看过一些文档,但我不知道缺少什么。
我的数据库称为测试,表称为testing1,只有用户名(varchar 25)和密码(文本)值。我究竟做错了什么?

编辑:回溯

Traceback (most recent call last):
  File "MySQLTesting.py", line 54, in <module>
    register()
  File "MySQLTesting.py", line 33, in register
    cur.execute(add_user, data_test)
  File "C:\Panda3D-1.8.0\python\lib\site-packages\MySQLdb\cursors.py", line 202,
 in execute
    self.errorhandler(self, exc, value)
  File "C:\Panda3D-1.8.0\python\lib\site-packages\MySQLdb\connections.py", line
36, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax
; check the manual that corresponds to your MySQL server version for the right s
yntax to use near 'username, passwordVALUES ('Thing', 'idk')' at line 1")

最佳答案

根据doc's,您应将列名称括在括号中:

add_user = "INSERT INTO testing1 (username, password) VALUES (%s, %s)"

关于python - MySQLdb注册程序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28528304/

10-16 12:04