首页 > 自己写的Flask mysql操作类出错?

自己写的Flask mysql操作类出错?

操作类如下

#coding=utf-8
import MySQLdb as sDB
class DBc:
    dbs=None
    con=None
    cur=None
    def __init__(self,Mysql_I):        
        self.dbs=Mysql_I
        self.con=sDB.connect(self.dbs['DB_HOST'] , self.dbs['DB_USER'],self.dbs['DB_PASS'], self.dbs['DB_TABLE'])
        self.cur=self.con.cursor()    
    def QueryS(self,sql):
        res=self.cur.execute(sql)
        self.con.commit()
        self.cur.close()
        self.con.close()
        return res
        

然后在另外一个文件里这样调用

@app.route("/")
def hello():
    ss=DBc(bst)
    s=ss.QueryS("INSERT INTO test(Name) VALUES('testName1521')")
    return s

运行时出错,但数据确实写到了数据库里
主要的错误信息是:
TypeError: 'long' object is not callable
看了好久都不知道哪里错了,网上找大多都是用的flask_sqlalchemy,很少直接用mysql的,我觉得写这样一个小博客用不着那些东西,希望大家能指导下,谢谢了.


注意: execute 返回的并不是查询结果,而是影响的行数(affected rows)

而flask的视图函数只能接受字符串或response对象作为返回值(详见: http://flask.pocoo.org/docs/0.10/quickstart/#about-responses ),从而引发了这个错误

关于MySQL-python的正确使用方法,参考: http://mysql-python.sourceforge.net/MySQLdb-1.2.2/


另建议:

【热门文章】
【热门文章】