必须是字符串或只读缓冲区,不能长

时间:2016-07-01 09:02:03

标签: python phpmyadmin mysql-python

我正在使用flask创建一个网站,下面的MySQLdb是我的python文件

init.py

from flask import Flask,render_template,request,url_for,flash,session
from flask_session import Session
from dbconnect import connection
from wtforms importForm,BooleanField,StringField,TextField,PasswordField,validators,IntegerField
from passlib.hash import sha256_crypt
from MySQLdb import escape_string as thwart
from flask_wtf import Form
from wtforms.validators import  InputRequired
import gc

sess=Session()
SESSION_TYPE = 'memcache'
app=Flask(__name__)

@app.route('/')
def test():
    return render_template("home.html")


@app.route('/about/')
def about():
    return render_template("about.html")

@app.route('/dashbord/')
def dashbord():
    return('hello')


@app.route('/contact/')
def contact():
    return render_template("contact.html")

@app.route('/login/',methods=['GET','POST'])
def login():
    return render_template("login.html")


class RegistrationForm(Form):
    username=TextField('username',[validators.Length(min=4,max=20),validators.Required()])
    email=TextField('email',[validators.Length(min=6,max=50),validators.Required()])
    password=PasswordField('password',[validators.EqualTo('confirm',message="Password must match"),validators.Required()])
    confirm=PasswordField("repeat password")
    phone_no=IntegerField('phone_no',[validators.Required()])


@app.route("/sign_up",methods=['GET','POST'])
def sign():
    try:
        form=RegistrationForm(request.form)
        if request.method == 'POST':
            username=form.username.data
            email=form.email.data
            password=sha256_crypt.encrypt((str(form.password.data)))
            phone_no=form.phone_no.data
            c,conn =connection()

            x = c.execute("SELECT * FROM customer WHERE username =(%s)",
                            (username,))

            if int(x)>0:
                flash("That username is taken")
                return render_template('sign.html',form=form)
            else:
                args="INSERT INTO customer (username,email,password,phone_no) VALUES (%s,%s,%s,%s)",
                (thwart(username),thwart(email),thwart(password),thwart(phone_no))
                c.execute(*args)

                conn.commit()
                flash("Thanks for registering")
                c.close()
                conn.close()

                gc.collect()

                session['logged_in']=True
                session['username']=username
                return redirect(url_for('dashbord'))
        return render_template("sign.html",form=form)

    except Exception as e:
        return(str(e))



if __name__=="__main__":
    app.secret_key = 'super secret key'
    app.config['SESSION_TYPE'] = 'filesystem'
    sess.init_app(app)
    app.run(debug=True)

点击提交按钮后,我收到以下错误

  

必须是字符串或只读缓冲区,不能长

1 个答案:

答案 0 :(得分:0)

你的cursor.execute看起来不正确,因为你的情况是c.execute,因为可选值有一个额外的逗号。 如果我错了请纠正我