CentOS 7.2 安装 python 读 mysql

前端之家收集整理的这篇文章主要介绍了CentOS 7.2 安装 python 读 mysql前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、系统自带python 2.7


2、安装MysqL

点击打开链接


3、安装 MysqLdb 库

yum install MysqL-python

在python客户端导入看是否报错:

import MysqLdb


4、自己先建一个users表:

import MysqLdb
import tornado.ioloop
import tornado.web

cxn = MysqLdb.Connect(host = '127.0.0.1',user = 'root',passwd = '')

class Test(tornado.web.RequestHandler):
    def get(self):
        result = ""
        cur = cxn.cursor()
        cur.execute("USE test")
        cur.execute("SELECT * FROM users")
        for row in cur.fetchall():
            result += row.__str__()
        print(result)
        self.write(result)

class Login(tornado.web.RequestHandler):
    def get(self):
        userName = self.get_argument('userName','hello')
        passWord = self.get_argument('passWord','hello')
        sql = "SELECT * FROM users WHERE username='%s'" %(userName)
        cur = cxn.cursor()
        cur.execute("USE test")
        cur.execute(sql)
        result = cur.fetchall()
        cur.close()
        cxn.commit()
        if result[0][2] == passWord:
            self.write("1")
        else:
            self.write("0")

application = tornado.web.Application([
    (r"/test",Test),(r"/login",Login)
])

if __name__=="__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
    cxn.close()
原文链接:https://www.f2er.com/centos/377875.html

猜你在找的CentOS相关文章