(13)python连接msql

python连接mysql两种方法

一、python官网提供的 MySQL-python 软件

下载地址

https://pypi.python.org/pypi/MySQL-python/1.2.5     (MySQL-python-1.2.5.win32-py2.7.exe )

http://download.csdn.net/detail/seven_zhao/6607625(MySQL-python-1.2.3.win-amd64-py2.7)

安装时如果报一下错误

Python version 2.7 required, which was not found in the registry

用一下方法解决

方法:转自(http://www.cnblogs.com/min0208/archive/2012/05/24/2515584.html)

新建一个register.py 文件,把一下代码贴进去,保存(G盘)

#
# 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()

安装成功后,

1、导入模块

import MySQLdb 

如果没报错说明安装成功

2、创建连接对象,传入参数

conn=MySQLdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘abc‘,db=‘db_meng‘,port=3306)

3、创建游标对象并获取数据库连接

cur=conn.cursor()

4、执行sql

增删改和建库建表的纯sql语句

cur.execute()

例如:

cur.execute(‘CREATE TABLE tb_meng7(id INT,name1 VARCHAR(50)‘)

5、关闭游标

cur.close()

6、提交事物

conn.commit()

7、关闭数据库连接

conn.close()

8、显示查询数据

每次只显示一行,游标向后移动一位

cur.fetchone()

9、查询结果,游标控制

定位到查询的第一条数据

cur.scroll(0,‘absolute‘)

10、获得多条数据,必须要指定数据条数

info = cur.fetchmany(i)

然后用遍历的方法,得出所有数据

for ii in info:
    print ii

完整代码:

二、mysql connector python v2.1.6 for python v2.7

mysql 5.7.18.1 里附带的软件(别的版本没用过,只能拿这个举例子)

mysql下载地址 https://dev.mysql.com/downloads/mysql/

安装时选自定义可以看到此选项

安装成功后

import mysql.connector

如果没报错说明安装成功

时间: 2024-07-28 21:52:33

(13)python连接msql的相关文章

Python - 连接msql数据库

可能出现的异常:python中出现SyntaxError: Non-UTF-8 code 解决方法 刚开始一直有用idle写代码,今天用了一下PyDev结果发现中文不支持,在网上搜了一下,结果发现解决方法都写得比较乱,自己写一个记录一下. 1.把相应Python程序文件的编码转成UTF-8格式就可以了.以Eclipse+PyDev为例: 在左侧Package Explorer里面找到相应的文件,点击右键选择"Properties" 把编码从默认: 改为: 再添入中文内容就可以正常运行了

Ubuntu12和13版本连接ios7设备会出现循环提示“是否信任这台电脑”

Ubuntu12和13版本连接ios7设备会出现循环提示“是否信任这台电脑”,这一问题在ubuntu14.10得到了解决. 以下是ubuntu12.04解决这一问题的方法: 所有方法的实质都是将libimobiledevice这个库升级到1.1.6版本,由于这一版本是非稳定版本,所以机器上默认安装的都是1.1.4稳定版本. 1.网上很多人说可以使用加一个ppa的源,然后update+upgrade的方法进行,但是通常给出的ppa源都会404 notfound.但是这里留下一些ppa源: sudo

Python 连接 Oracle数据库

1.环境设置 [[email protected] ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [[email protected] ~]# python -V Python 2.6.6 版本:Oracle 12c 2.前提:安装cx_Oracle模块依赖包 由于使用Python连接Oracle,所以需要下载oracle客户端包 官网:http://www.oracle.com/technetwork/topics/linuxx8

Python连接Oracle实例

最近需要用Python连接Oracle操作,之前接触过,没自己手写过,特此记录.数据库是Oracle 11,python版本2.7的. 这个需求是两个库A和B,假设现在需要识别一张表在A库里面有没记录,如果没有,将表名写入一个log,如果有再去B库里查有没有该表,如果没有,表名计入另外一个log文件. 查库的sql语句 select t.* from all_objects t where t.object_name = upper(&table_name); Python连接oracle需要的

13.1 设置更改root密码;13.2 连接MySQL;13.3 MySQL常用命令

扩展 : mysql5.7 root密码更改 http://www.apelearn.com/bbs/thread-7289-1-1.html myisam 和innodb引擎对比 http://www.pureweber.com/article/myisam-vs-innodb/ mysql 配置详解: http://blog.linuxeye.com/379.html mysql调优: http://www.aminglinux.com/bbs/thread-5758-1-1.html 同学

python连接mongodb并操作

安装python连接mongodb的库文件pymongo pip install pymongo python连接mongodb程序 import pymongo conn = pymongo.MongoClient("ip",端口) db = conn.admin #连接库 db.authenticate("账号","密码") #用户认证 db=conn.jwh db.test.insert({'id':1,'name':'kaka','sex

python连接字符串的方式

发现Python连接字符串又是用的不顺手,影响速度 1.数字对字符进行拼接 s=""  #定义这个字符串,方便做连接 print type(s) for i in range(10): print i type(i) s+=str(i)  #转换类型在对接 print s 2.字符对字符进行拼接 string="abcdef" for i in string: print i+'jun'  直接使用字符串连接 3.列表和字符串的拼接 list1=['hello','

python 连接sql server数据库的示例代码

首先,到http://pymssql.sourceforge.net/下载pymssql模块,必须安装这个模块才可以用python连接mysql 以下是sql server的操作代码,需要注意字符集 # -*- coding:utf-8 -*- import pymssql #创建一个数据库连接,host是服务器的ip地址,如果是本机可以用".",user是访问用户名,password是密码,database是数据库名 conn=pymssql.connect(host=".

Centos6.7下安装python连接mysql环境故障解决

在Python连接和使用mysql时,需要调用MySQLdb 模块,是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的. MySQLdb模块在不同的平台下安装方式不一样,这里主要介绍Linux下的安装方式,https://pypi.python.org/pypi/MySQL-python  可以在这里选择合适的版本,解压安装. 安装步骤: 1.选择合适的版本以及检查相关路径 这里选择版本为MySQL-pyt