MySQLdb 注册程序?

标签: MySQL Python
发布时间: 2016/10/13 23:31:23
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

所以我和我朋友想要一起学习 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:

根据医生的你应该穿你列名称在括号中︰

add_user = "INSERT INTO testing1 (username, password) VALUES (%s, %s)"
官方微信
官方QQ群
31647020