mac下安装MySQL-python
来源:互联网 发布:php-fpm 扩展 编辑:程序博客网 时间:2024/05/23 18:34
pip uninstall mysql-python
brew uninstall mysql-connector-c
brew install mysql-connector-c
pip install mysql-python
还有中文编码的1366错误,大家也可以看一下.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
import time
from subprocess import Popen,PIPE
class mysqlobj():
def __init__(self,addr = 'localhost',port = 3306,usname = 'root',uspw = '123456',defDB = 'test'):
self.mysqladdr = addr #mysql地址
self.mysqlport = port #mysql端口
self.mysqlusername = usname #mysql登陆用户名
self.mysqlpassword = uspw #mysql登陆密码
self.mysqlDefaleDB = defDB #mysql默认登陆数据库
self.connectManger = None #mysql连接管理器
self.mysqlcursor = None #mysql消息收发器
self.connectMysql() #连接mysql数据库
def connectMysql(self):
self.connectManger = MySQLdb.connect(
host = self.mysqladdr,
port = self.mysqlport,
user = self.mysqlusername,
passwd = self.mysqlpassword,
db = self.mysqlDefaleDB,
charset="utf8". #注意这里的utf8命令编码格式要设置
)
self.mysqlcursor = self.connectManger.cursor()
#调用mysql命令
def execute(self,cmdstr,isCommit = True):
try:
self.connectManger.ping()
except:
self.connectMysql()
if self.mysqlcursor:
print 'mysqlask:%s'%(cmdstr)
try:
tmp = self.mysqlcursor.execute(cmdstr)
if isCommit:
self.connectManger.commit()
except MySQLdb.Error, e:
print MySQLdb.Error,e,time.ctime(time.time())
if MySQLdb.Error == 2006:
self.connectMysql()
print MySQLdb.Error
if MySQLdb.Error and e[0] == 1050:#数据表已存在,表创建错误
print '数据表已存在,mysql错误码:1050'
return 1050
elif MySQLdb.Error and e[0] == 2014:
self.mysqlcursor.close()
self.mysqlcursor= self.connectManger.cursor()
return 2014
else:
tmp = self.mysqlcursor.execute(cmdstr)
if isCommit:
self.connectManger.commit()
return tmp
else:
return -999#mysql 未连接#使用.sql文件
def inPutDataWithSqlFile(self,sqlfilepath):
process = Popen('/usr/local/mysql/bin/mysql -h%s -P%s -u%s -p%s %s' %(self.mysqladdr, self.mysqlport, self.mysqlusername, self.mysqlpassword, self.mysqlDefaleDB), stdout=PIPE, stdin=PIPE, shell=True)
output = process.communicate('source '+sqlfilepath)
print output
- mac下安装python-mysql
- mac下安装mysql-python
- Mac下安装MySQL-Python
- mac下安装MySQL-python
- mac下安装和使用MySQL-python
- Mac下安装mysql-python失败解决方法
- mac下安装和使用MySQL-python
- Mac OSX下安装MySQL-python(Python访问MySQL)
- mac 安装MySQL-python
- mac下安装mysql
- mac下安装MySQL
- Mac下安装mysql
- Mac 下安装Mysql
- Mac下安装MySQL
- Mac下安装MySQL
- mac 下安装mysql
- mac下安装mysql
- Mac 下安装 MySql
- 在学习深度神经网络知识时想到的
- 股票数据接口
- Fluent Python读书笔记(二)
- TensorFlow 1.0更新python相关api更改
- 使用程序分析股票数据
- mac下安装MySQL-python
- 深入编译链接和运行
- mysql-python中文数据错误
- java基础知识常见错误集锦
- UVA
- 第13章 字符串
- 推荐几个tor搜索引擎
- sublime text 3的sql插件
- sql存储过程