(15)python 数据库连接
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/distutils-sig@python.org/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
如果没报错说明安装成功
(15)python 数据库连接的更多相关文章
- python数据库连接池
python数据库连接池 import psycopg2 import psycopg2.pool dbpool=psycopg2.pool.PersistentConnectionPool(1,1, ...
- Python天天美味(15) - Python正则表达式操作指南(re使用)(转)
http://www.cnblogs.com/coderzh/archive/2008/05/06/1185755.html 简介 Python 自1.5版本起增加了re 模块,它提供 Perl 风格 ...
- Python数据库连接池DBUtils.PooledDB
DBUtils 是一套用于管理数据库连接池的包,为高频度高并发的数据库访问提供更好的性能,可以自动管理连接对象的创建和释放.最常用的两个外部接口是 PersistentDB 和 PooledDB,前者 ...
- Python数据库连接池---DBUtils
Python数据库连接池DBUtils DBUtils是Python的一个用于实现数据库连接池的模块. 此连接池有两种连接模式: 模式一:为每个线程创建一个连接,线程即使调用了close方法,也不 ...
- Python数据库连接池DBUtils
Python数据库连接池DBUtils DBUtils是Python的一个用于实现数据库连接池的模块. 此连接池有两种连接模式: 模式一:为每个线程创建一个连接,线程即使调用了close方法,也不 ...
- 15.python的for循环与迭代器、生成器
在前面学习讲完while循环之后,现在终于要将for循环这个坑填上了.之所以拖到现在是因为for循环对前面讲过的序列.字典.集合都是有效的,讲完前面的内容再来讲for循环会更加容易上手. 首先,for ...
- Python数据库连接池实例——PooledDB
不用连接池的MySQL连接方法 import MySQLdbconn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='my ...
- python数据库连接池设计
一.背景: 传统访问资源,一般分为一下几个步骤: 1.实例数据驱动对象与链接资源.2.实例操作资源游标.3.获取资源.4.关闭链接资源. 根据以上步骤,我们可以很简单使用这个原始方法来访问资源为我们业 ...
- 15.python并发编程(线程--进程--协程)
一.进程:1.定义:进程最小的资源单位,本质就是一个程序在一个数据集上的一次动态执行(运行)的过程2.组成:进程一般由程序,数据集,进程控制三部分组成:(1)程序:用来描述进程要完成哪些功能以及如何完 ...
随机推荐
- 收藏一个漂亮的Flash焦点图切换
网上闲逛的时候发现一个Flash焦点图效果,跟喜欢,然后就下载回来,收集在这里,以便以后方便取用.这个Flash使用方法也是相当简单的,如果你喜欢,也可以从这里查看源代码下载. Flash 焦点图效果 ...
- 给DOM元素绑定click事件也有学问
最简单的莫过于使用click方法: 1 <input id="btn" type="button" value="BUTTON" on ...
- Codeforces Round #524 (Div. 2) D. Olya and magical square
D. Olya and magical square 题目链接:https://codeforces.com/contest/1080/problem/D 题意: 给出一个边长为2n的正方形,每次可以 ...
- es6+最佳入门实践(12)
12.class基础用法和继承 12.1.class基础语法 在es5中,面向对象我们通常写成这样 function Person(name,age) { this.name = name; this ...
- 转载:Apache commons开源工具简介
Apache Commons是一个非常有用的工具包,解决各种实际的通用问题,下面是一个简述表,详细信息访问http://jakarta.apache.org/commons/index.html Be ...
- 常用原生客户端js
var el = document.createElement('pre'); // 创建 <pre></pre>元素 el.id = 'sss'; // 添加id <p ...
- 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)
本文属于本人原创,转载请注明出处:http://blog.csdn.net/xxd851116/archive/2009/06/25/4296866.aspx [前面的话] 在网上经常看到有人对req ...
- 【BZOJ2326】【HNOI2011】数学作业 [矩阵乘法][DP]
数学作业 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Input 输入文件只有一行为用空 ...
- noip车站分级 拓扑排序
题目传送门 这道题呢 每次输入一段数就把1~n里面没有在这组数里面的数和他们连一波 表示这些数比他们等级低 然后就搞一搞就好了哇 #include<cstdio> #include< ...
- 【BZOJ】1571: [Usaco2009 Open]滑雪课Ski
[算法]动态规划 [题解]yy出了O(1w log 1w)的算法. 将雪坡排序预处理出g[i]表示能力值为i的最短时长雪坡. 这样就可以定义work(t,c)表示时长t能力c的最多滑雪数量,work( ...