一、在shell窗口:
安装mysql-python包:
$ sudo yum install MySQL-python
二、在pycharm
安装mysql-python包:file—>setting—>project interpreter—> "+" —>输入mysqldb—>install pakage。
import MySQLdb
class 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.()