我該怎么做這樣的事情,我想檢查用戶是否存在于 python 中的表中,如果用戶存在,它應該報告特定用戶存在,否則如果用戶不存在,它應該注冊(插入用戶進入mysql資料庫)
到目前為止,這就是我的代碼的樣子
@app.route('/api/user',methods=['POST'])
def create_user():
_json = request.json
_email = _json['email']
_phone = _json['phone']
_password = _json['password']
fullname = 'NULL'
custID = '123456'
#conn = mysql.connect()
#cursor = conn.cursor(pymysql.cursors.DictCursor)
cursor = mysql.connection.cursor()
checkuser = 'select email from accounts where email = %s' # check if user exists here.
query = "insert into accounts (email,phone,fullname,password,custID) values (%s, %s,%s, %s,%s)"
#query = "update empData set name = %s, email = %s, phone = %s, address = %s, salary = %s"
bindData = (_email, _phone, _password , fullname , custID)
cursor.execute(query,bindData)
mysql.connection.commit()
cursor.close()
output = {'email':_email, 'phone':_phone, 'fullname':fullname, 'custID':custID, 'message':'ok'}
return jsonify({'result':output}),200
我該怎么做這樣的事情,我一周前開始使用燒瓶。
編輯
這是我一直在做的,但它抱怨縮進。代碼看起來像這樣
@app.route('/api/user', methods=['POST'])
def create_user():
_json = request.json
_email = _json['email']
_phone = _json['phone']
_password = _json['password']
fullname = 'NULL'
custID = '123456'
cursor = mysql.connection.cursor()
checkuser = 'select email from accounts where email = %s'
bindData = (_email)
cursor.execute(query,bindData)
acc = cursor.fetchone()
if acc:
return jsonify({'message':'User exists, Please Login'})
elif:
query = "insert into accounts (email,phone,fullname,password,custID) values (%s, %s,%s, %s,%s)"
bindData = (_email, _phone, _password , fullname , custID)
cursor.execute(query,bindData)
mysql.connection.commit()
cursor.close()
output = {'email':_email, 'phone':_phone, 'fullname':fullname, 'custID':custID, 'message':'ok'}
return jsonify({'result':output}),200
編輯 2
因此,我第二次進行了一些編輯,當我使用 Postman 進行測驗時,它只會觸發錯誤 500。
我的代碼正在尋找
@app.route("/api/user", methods=["POST"])
def create_user():
_json = request.json
_email = _json["email"]
_phone = _json["phone"]
_password = _json["password"]
fullname = "NULL"
custID = "123456"
cursor = mysql.connection.cursor()
cursor.execute('select * from accounts where email = %s', _email)
acc = cursor.fetchone()
if acc:
return jsonify({"message": "User exists, Please Login"})
else:
query = "insert into accounts (email,phone,fullname,password,custID) values (%s, %s,%s, %s,%s)"
bindData = (_email, _phone, _password, fullname, custID)
cursor.execute(query, bindData)
mysql.connection.commit()
cursor.close()
output = {
"email": _email,
"phone": _phone,
"fullname": fullname,
"custID": custID,
"message": "ok",
}
return jsonify({"result": output}), 200
它說這是根據日志的錯誤所在
這是cursor.execute('select * from accounts where email = %s', _email)我錯過了什么嗎?
uj5u.com熱心網友回復:
幾周前我做了一個類似的程式,它是相同的概念,但方法有點初級,我希望它有所幫助。
假設 SQL 連接設定正確,在我的情況下使用表“userdata”并搜索列“username”
def login(user):
cursor.execute("SELECT * FROM userdata WHERE username = '%s';" %(user,))
record = cursor.fetchone()
if record != None: # record = ('<user>','<password>')
if record[1]==password:
login_success()
else:
login_failed()
else:
data_not_found()
這在按下按鈕后激活。
login_btn = Button(root,text='Login',command=lambda:[del_failed_msg(),get_input(),login(name)])
因此,在這里,資料庫中的搜索結果應該是我使用 fetchone() 函式存盤在變數“記錄”下的單個記錄。fetchone() 函式回傳了我想要的搜索的元組,我可以遍歷它以在記錄中獲取我想要的值。
uj5u.com熱心網友回復:
我讓它作業了!我不得不做一些閱讀和搜索,這讓我知道該怎么做。就像在資料庫中搜索串列或其他內容以獲得足夠的結果
所以我看到了這個,https://stackoverflow.com/questions/21740359/python-mysqldb-typeerror-not-all-arguments-converted-during-string-formatting
然后從這里更改了我的代碼
cursor.execute('select * from accounts where email = %s', _email
對此:
cursor.execute('select * from accounts where email = %s', [_email]
它給出了我希望它給出的實際回應。以防萬一它應該幫助某人。
感謝大家。
編輯
下面是變通后代碼的樣子。
@app.route("/api/user", methods=["POST"])
def create_user():
_json = request.json
_email = _json["email"]
_phone = _json["phone"]
_password = _json["password"]
fullname = "NULL"
custID = "123456"
cursor = mysql.connection.cursor()
cursor.execute("select * from accounts where email = %s", [_email])
acc = cursor.fetchone()
if acc:
return jsonify({"message": "User exists, Please Login"})
else:
query = "insert into accounts (email,phone,fullname,password,custID) values (%s, %s,%s, %s,%s)"
bindData = (_email, _phone, fullname, _password, custID)
cursor.execute(query, bindData)
mysql.connection.commit()
cursor.close()
output = {
"email": _email,
"phone": _phone,
"fullname": fullname,
"custID": custID,
"message": "ok",
}
return jsonify({"result": output}), 200
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/525192.html
