Python2.7(windows 7 64bit) MySQLdb模块安装使用和遇到的问题

1.下载MySQL-python-1.2.3.win-amd64-py2.7.exe [这个模块的名字叫MySQL,但是安装包叫这个名] http://www.codegood.com/archives/129
2.安装MySQL-python-1.2.3.win-amd64-py2.7.exe,直接双击这个安装包,点下一步
这里写图片描述
接下来可能会报错,反正我的是报错了,说是找不到注册的python2.7,可以通过这样解决:
<1>在C盘下新建register.txt文件,然后修改文件名为register.py,然后用noteppad++打开
<2>将以下代码复制进去保存
<3>然后在命令行界面切到C盘下,执行python27 register.py 出现红框里的字就算注册成功了

#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/[email protected]/msg10512.html

import sys

from _winreg import *

# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix

regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
    installpath, installpath, installpath
)

def RegisterPy():
    try:
        reg = OpenKey(HKEY_CURRENT_USER, regpath)
    except EnvironmentError as e:
        try:
            reg = CreateKey(HKEY_CURRENT_USER, regpath)
            SetValue(reg, installkey, REG_SZ, installpath)
            SetValue(reg, pythonkey, REG_SZ, pythonpath)
            CloseKey(reg)
        except:
            print "*** Unable to register!"
            return
        print "--- Python", version, "is now registered!"
        return
    if (QueryValue(reg, installkey) == installpath and
        QueryValue(reg, pythonkey) == pythonpath):
        CloseKey(reg)
        print "=== Python", version, "is already registered!"
        return
    CloseKey(reg)
    print "*** Unable to register!"
    print "*** You probably have another Python installation!"

if __name__ == "__main__":
    RegisterPy()

这里写图片描述
接下来继续安装:要看好自己的python2.7的安装路径对不对,我的是D:\python2.7,然后再点击下一步,点击下一步就完成了。[完成图我就不发了]
这里写图片描述
3.因为我要连得是我虚拟机centos的mysql5.7,所以先到数据库里修改mysql库里的user表,我这里因为修改过了,所以不再修改了。代码里我有相关的sql修改语句。[一定要确保root的host是%,别问我为什么,当你遇到pycharm 2016连接数据库的时候报1045的错时,你就知道了]

mysql -uroot -p123456
mysql>use mysql;
mysql>select Host,User,authentication_string from user order by Host desc,User desc;

这里写图片描述

mysql>delete from user where Host='root';
mysql>grant all privileges on *.* to 'root'@'%' identified by '123456';
mysql>flush privileges;  #这一步不可缺少

4.接下来可以很愉快的连接mysql5.7啦[我用的pycharm2016],

import MySQLdb as mdb
conn = mdb.connect(host=192.168.66.132,port=3306,user='root',passwd='123456',db='test')#3306不能用单引号引住
cursor = conn.cursor()  #获取游标
<1>cursor.execute("select * from test.student") #执行查询语句
for i in cursor.fetchall():
    print i  #遍历出所有记录
<2>sql = "insert into test.student(num,name) values(%s,%s)" #插入语句
param = (6,'sd')
cursor.execute(sql,param)
cursor.execute("select * from test.student")
for i in cursor.fetchall():
   print i  #查看结果
<3>sql = "update test.student set name=%s where num=3" #更新
param = ("bbb")
n = cursor.execute(sql,param)
print n
cursor.execute("select * from test.student")
for i in cursor.fetchall():
   print i
<4>sql = "delete from test.student where num=%s"  #删除
param = (1)
cursor.execute(sql,param)
<5>sql = "drop table if exists %s"    #删除表
param = ('student')
cursor.execute(sql %param)
<6>sql = "drop database if exists %s" %('test')  #删除数据库
cursor.execute(sql)
<7>sql = "create database if not exists %s" %('test')  #创建数据库
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()

猜你喜欢

转载自blog.csdn.net/qq_42794720/article/details/82019441