def fetchuserlist(): conn = getconnection() try: c = conn.cursor() c.execute("select * from usertableofheros") value = c.fetchall() return value except BaseException: conn.rollback() mylogger.debug(BaseException + "Error happenedd") finally: conn.close()
def updateWeapon(weaponid): conn = getconnection() try: c = conn.cursor() c.execute('update weapontable set val=val+20 where weaponid=%s', (weaponid)) conn.commit() return resultStatus("update success") except: conn.rollback() mylogger.debug("update error") return resultStatus("update error") finally: conn.close()
def adduser(user): username = user.username password = user.password if (len(username) == 0 or len(password) == 0): return resultStatus("length of string is zero") if (username is None or password is None): return resultStatus("None data") conn = getconnection() try: c = conn.cursor() c.execute( 'insert into usertableofheros (username,password) values(%s,%s)', (username, password)) conn.commit() return resultStatus("insert success") except: mylogger.debug(BaseException) conn.rollback() return resultStatus("error happened") finally: conn.close()
def getconnection(): # try: # with pymysql.Connect(host="127.0.0.1",user='******', password='******', db="herogame",charset="utf8mb4") as conn: # print(type(conn)) # return conn # except: # return try: conn = pymysql.Connect(host="127.0.0.1", user='******', password='******', db="herogame", charset="utf8mb4") if (not conn): mylogger.debug("connect failed") print("connect failed,quit()") quit() else: return conn except: print("connect failed,quit()") quit()
#!python # -*- coding: UTF-8 -*- #%% import cgi, cgitb import json import dbUtil from pojo import User # 创建 FieldStorage 的实例化 import mylogger # 获取数据 cgitb.enable(display=0, logdir="f:\logdir") form = cgi.FieldStorage() username = form.getvalue("username") password = form.getvalue("password") mylogger.debug(username) mylogger.debug(password) try: result=dbUtil.adduser(User(username,password)) jsonobject=json.dumps(result) print("Content-type:json") print() print(jsonobject) except BaseException: mylogger.debug("service throw excepttion"+result) finally: pass