MySQLdb注册程序?

时间:2015-02-15 16:30:57

标签: python mysql mysql-python

所以我和一个朋友正在尝试一起学习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")

1 个答案:

答案 0 :(得分:0)

根据doc's,你应该用括号括起你的列名:

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