Python实战之MySQL数据库操作

来源:互联网 发布:淘宝客服工作在哪里找 编辑:程序博客网 时间:2024/05/12 05:02

一、在shell窗口:

 安装mysql-python包:

  $ sudo yum install MySQL-python

二、在pycharm

安装mysql-python包:file—>setting—>project interpreter—> "+" —>输入mysqldb—>install pakage。 

import MySQLdbclass MysqlManager:    def openConn(self, hoststr, userStr, passwdStr, tableSchema):        try:            conn = MySQLdb.connect(host='%s'%(hoststr), user='%s'%(userStr), passwd='%s'%(passwdStr), db='%s'%(tableSchema), port=3306, charset="utf8", use_unicode="True")            print "conn = ",conn            return conn        except Exception,e:            print str(e);    def select(self):        conn = self.openConn("192.168.0.110", "test", "test", "test")        cursor = conn.cursor()        cursor.execute("SELECT * FROM test")        rows = cursor.fetchall()        for row in rows:            print row        cursor.close ()        conn.close ()mysqlSelect = MysqlManager()mysqlSelect.()

0 0