python 和 mysql连接

虫师教程:http://www.cnblogs.com/fnng/p/3565912.html

其他教程pymysql:http://www.cnblogs.com/lcj0703/p/5712788.html

第一步,下载源代码:

# 请下载zip源代码,这样才可以编译
https://pypi.python.org/pypi/MySQL-python/1.2.5 # 我的版本从2.7升级到3.5.2的时候,上面那个就无效了。我使用了这个
https://pypi.python.org/pypi/PyMySQL

第二步,解压并且进入目录,输入编译命令:python setup.py install

如果编译有问题,根据指示解决

1、如需要c++
https://www.microsoft.com/en-us/download/confirmation.aspx?id=44266 2、如果出现这个错误:_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
error: command 'C:\\Users\\qinwanxia\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit status 2

需要下载并安装MySQL connector 32位,系统是64位的也需要安装32位:https://dev.mysql.com/downloads/connector/c/6.0.html#downloads

第三步,进入python命令行模式,检查是否包是否可引用:

python

import MySQLdb 
# 或者
import pymysql

第四步,在程序中使用mysql:

#coding=utf-8
import MySQLdb conn= MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='123456',
db ='test',
)
cur = conn.cursor() #创建数据表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))") #插入一条数据
#cur.execute("insert into student values('2','Tom','3 year 2 class','9')") #修改查询条件的数据
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'") #删除查询条件的数据
#cur.execute("delete from student where age='9'") cur.close()
conn.commit()
conn.close()

pymysql版本

import pymysql

#连接数据库
conn = pymysql.connect(host='192.168.8.208', port=,user = 'root', passwd='tuandai_bm2015', db='tuandai_bm') #创建游标
cur = conn.cursor() #查询lcj表中存在的数据
cur.execute("select * from tb_parameter where para_type='version' and para_name='H5'"); #fetchall:获取lcj表中所有的数据
ret1 = cur.fetchall() print(ret1)

设置pymysql默认字符集类型

找到C:\Python27\Lib\site-packages\pymysql安装目录,使用文本编辑器(如notepad++)打开connections.py,我的在550行有这样一句话,你想使用的字符集。

python 和 mysql连接的更多相关文章

  1. python 链接mysql 连接池

    # python 链接mysqlimport mysql.connector.poolingconfig = { "host":"localhost", &qu ...

  2. python&pandas 与mysql 连接

    1. python 与mysql 连接及操作,直接上代码,简单直接高效: import MySQLdb try: conn = MySQLdb.connect(host='localhost',use ...

  3. Python 3.5 连接Mysql数据库(pymysql 方式)

    由于 MySQLdb 模块还不支持 Python3.x,官方的Mysql连接包只支持到3.4,所以 Python3.5 如果想连接MySQL需要安装 pymysql 模块. pymysql 模块可以通 ...

  4. python学习之 -mysql 连接和db_config配置

    最近学习python,记录下自己写学习python的代码和心得,自己写了一个使用python mysql 的查询语句和做的一个db_config.py 配置信息. 1.db_config.py 配置文 ...

  5. python的Web框架,Django的ORM,模型基础,MySQL连接配置及增删改查

    Django中的ORM简介 ORM概念:对象关系映射(Object Relational Mapping,简称ORM): 用面向对象的方式描述数据库,去操作数据库,甚至可以达到不用编写SQL语句就能够 ...

  6. 【Python】Windows平台下Python、Pydev连接Mysql数据库

    Mysql数据库是跨平台的,不是说Python一定就要连接Mongodb. Python连接Mysql数据库是非常easy的. 首先,你要配置好Python的开发环境,详见<[Python]Wi ...

  7. python中实现mysql连接池

    python中实现mysql连接池 import pymysql from DBUtils.PooledDB import PooledDB MYSQL_HOST = 'localhost' USER ...

  8. python使用sqlalchemy连接mysql数据库

    环境:centos7+python2.7.5+sqlalchemy sqlalchemy是python当中比较出名的orm程序.在python中,使用sqlalchemy连接mysql数据库进行操作非 ...

  9. Python和mysql的连接

    python与mysql的连接: 说明:前提是已近安装了mysql以及可视化工具(本人装的是Navicat) 1.在cmd下下载Python的第三方数据库包:pip install pymysql: ...

随机推荐

  1. webstorm配置autoprefix

    http://blog.csdn.net/pugongying520/article/details/52712639 配置图

  2. linux磁盘挂载流程

    参考 [https://www.jianshu.com/p/ea57fb7834f2]

  3. 【03】react 之 创建component

    React推出后,出于不同的原因先后出现三种定义react组件的方式,殊途同归:具体的三种方式: 函数式定义的无状态组件 es5原生方式React.createClass定义的组件 es6形式的ext ...

  4. Codevs 4633 [Mz]树链剖分练习

    4633 [Mz]树链剖分练习 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 大师 Master 题目描述 Description 给定一棵结点数为n的树,初始点权均为0,有依次q个 ...

  5. javascript获取querystring值【个人觉得这种方法最好最棒最像.NET】

    原文发布时间为:2009-05-22 -- 来源于本人的百度文章 [由搬家工具导入] JavaScript获取QueryString值, 当没有QueryString值时输出bool型 null 用j ...

  6. mvc filters

    1.controller using System; using System.Collections.Generic; using System.Linq; using System.Web; us ...

  7. [LeetCode] Find Peak Element 二分搜索

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  8. hdu 1847 Good Luck in CET-4 Everybody! 组合游戏 找规律

    题目链接 题意 有\(n\)张牌,两人依次摸牌,每次摸的张数只能是\(2\)的幂次,最后没牌可摸的人为负.问先手会赢还是会输? 思路 0 1 2 3 4 5 6 7 8 9 10 11 -- P N ...

  9. Android build code command

    make bootimage -j8 make systemimage -j8

  10. FormatDateTime 当前时间减去几小时的做法

    top_start_modified := FormatDateTime('yyyy-mm-dd hh:mm:ss',(Now - ((1/24)*3)));    top_end_modified ...