Python 操作 Mysql 模块的安装

linux:

    yum install MySQL-python

window:
http://files.cnblogs.com/files/wupeiqi/py-mysql-win.zip
unbuntu
  https://pypi.python.org/pypi/MySQL-python/1.2.5#downloads
  解压后pip setup.py build 如果报错 EnvironmentError: mysql_config not found
  sudo apt-get install libmysqlclient-dev
  sudo updatedb
  locate mysql_config
  mysql_config的位置为:/usr/bin/mysql_config   在mysql-python源码包下找到:setup_posix.py 文件,然后找到文件中的 mysql_config.path 将其值改为:/usr/bin/mysql_config,然后 sudo python    setup.py install ,就ok了

安装

SQL 基本使用:

1、数据库操作

show databases;
use [databasename];
create database [name];

2、数据表操作

show tables;

create table students
(
id int not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);
desc chen.student; #desc获取表结构

3、数据操作

insert into student(name,sex,age,tel) values("chen","man","","");

delete from student where id = 1;

update student set name = 'chen' where id =2;

select * from student;

Python MySQL API

import MySQLdb

# 连接到数据库
conn = MySQLdb.connect(host="127.0.0.1",user="root",passwd="chen27",db="chen")
cur = conn.cursor() #创建游标 #插入数据,注意mysql的占位符是%s(通过游标的方法execute插入数据)
recount = cur.execute('insert into student(name,sex,age,tel) values(%s,%s,%s,%s)',("chen27","man","","")) #提交事物
conn.commit()
# 关闭连接
cur.close #关闭游标
conn.close() # 关闭连接 print recount

批量插入数据

import MySQLdb

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='chen',db='mydb')

cur = conn.cursor()

li =[
('alex','usa'),
('sb','usa'),
]
reCount = cur.executemany('insert into UserInfo(Name,Address) values(%s,%s)',li) conn.commit()
cur.close()
conn.close() print reCount

二、删除数据

import MySQLdb

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')

cur = conn.cursor()

reCount = cur.execute('delete from UserInfo')

#conn.rollback() 回滚操作
conn.commit() cur.close()
conn.close() print reCount

三、修改数据

import MySQLdb

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')

cur = conn.cursor()

reCount = cur.execute('update UserInfo set Name = %s',('alin',))

conn.commit()
cur.close()
conn.close() print reCount

四、查数据

# ############################## fetchone/fetchmany(num)  ##############################

import MySQLdb

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
cur = conn.cursor() reCount = cur.execute('select * from UserInfo') print cur.fetchone() # 取一条 fetchall() 取所有 fetchmany(num) 指定多少条
print cur.fetchone()
cur.scroll(-1,mode='relative')
print cur.fetchone()
print cur.fetchone()
cur.scroll(0,mode='absolute')
print cur.fetchone()
print cur.fetchone() cur.close()
conn.close() print reCount # ############################## fetchall ############################## import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
#cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
cur = conn.cursor() reCount = cur.execute('select Name,Address from UserInfo') nRet = cur.fetchall() cur.close()
conn.close() print reCount
print nRet
for i in nRet:
print i[0],i[1]

一些参数

cursor用来执行命令的方法:

     callproc(self, procname, args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回值为受影响的行数
  execute(self, query, args):执行单条sql语句,接收的参数为sql语句本身和使用的参数列表,返回值为受影响的行数
  executemany(self, query, args):执行单挑sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数
  nextset(self):移动到下一个结果集 cursor用来接收返回值的方法:   fetchall(self):接收全部的返回结果行.
  fetchmany(self, size=None):接收size条返回结果行.如果size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据.
  fetchone(self):返回一条结果行.
  scroll(self, value, mode='relative'):移动指针到某一行.如果mode='relative',则表示从当前所在行移动value条,如果mode='absolute',则表示从结果集的第一 行移动value条.
 

Python Mysql 篇的更多相关文章

  1. Python自动化测试面试题-MySQL篇

    目录 Python自动化测试面试题-经验篇 Python自动化测试面试题-用例设计篇 Python自动化测试面试题-Linux篇 Python自动化测试面试题-MySQL篇 Python自动化测试面试 ...

  2. 【Python五篇慢慢弹】快速上手学python

    快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...

  3. python 之路,Day11(上) - python mysql and ORM

    python 之路,Day11 - python mysql and ORM   本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 ...

  4. Python入门篇-文件操作

    Python入门篇-文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.文件IO常用操作 open:打开 read:读取 write:写入 close:关闭 readlin ...

  5. Python入门篇-函数、参数及参数解构

    Python入门篇-函数.参数及参数解构 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.函数概述 1>.函数的作用即分类 函数 数学定义:y=f(x) ,y是x的函数,x ...

  6. python mysql中in参数化说明

    第一种:拼接字符串,可以解决问题,但是为了避免sql注入,不建议这样写 还是看看第二种:使用.format()函数,很多时候我都是使用这个函数来对sql参数化的 举个例子: select * from ...

  7. 测试面试题集锦(四)| Linux 与 Python 编程篇(附答案)

    本文为霍格沃兹测试学院学员学习笔记. 本系列文章总结归纳了一些软件测试工程师常见的面试题,主要来源于个人面试遇到的.网络搜集(完善).工作日常讨论等,分为以下十个部分,供大家参考.如有错误的地方,欢迎 ...

  8. Python自动化测试面试题-Python基础篇

    目录 Python自动化测试面试题-经验篇 Python自动化测试面试题-用例设计篇 Python自动化测试面试题-Linux篇 Python自动化测试面试题-MySQL篇 Python自动化测试面试 ...

  9. openresty 前端开发入门五之Mysql篇

    openresty 前端开发入门五之Mysql篇 这章主要演示怎么通过lua连接mysql,并根据用户输入的name从mysql获取数据,并返回给用户 操作mysql主要用到了lua-resty-my ...

随机推荐

  1. Web前端入门了解

    Web就是指万维网,网站,Web开发的方向包括Web前端,Web后台. Web前端又分为  静态Web 和 动态Web, 静态Web就是用HTML实现的. 动态Web的实现方式有多种. 动态Web的实 ...

  2. SSH原理与运用

    SSH是每一台Linux电脑的标准配置. 随着Linux设备从电脑逐渐扩展到手机.外设和家用电器,SSH的使用范围也越来越广.不仅程序员离不开它,很多普通用户也每天使用. SSH具备多种功能,可以用于 ...

  3. Android碎片使用

    首先新建一个fragment的布局文件,   <?xml version="1.0" encoding="utf-8"?><LinearLay ...

  4. MATLAB 图像处理——Contrast Enhancement Techniques

    Contrast Enhancement Techniques %调整图片尺寸imresizeimages{k} = imresize(images{k},[width*dim(1)/dim(2) w ...

  5. 向mysql workbench中导入.sql文件

    mysql workbench用的不多,前段时间装了一下,然后用了一下,感觉操作比dbdesigner4要更人性化一点.其中二个方面做了改进,让我觉得很爽. 第一,就是端口可以修改了,以前就是定死33 ...

  6. AdaBoosting 3

    在学习AdaBoosting和online Boosting, 最好有bagging和boosting基础,这样看起来比较会比较顺.有空再补上. AdaBoost 算法的主要思想之一就是在训练集上维护 ...

  7. Tri-Training: Exploiting Unlabeled Data Using Three Classifiers

    Abstract – In many practical data mining applications such as web page classification, unlabeled tra ...

  8. vim常用命令总结 (转)

    vim 选择文本,删除,复制,粘贴   文本的选择,对于编辑器来说,是很基本的东西,也经常被用到,总结如下: v    从光标当前位置开始,光标所经过的地方会被选中,再按一下v结束. V    从光标 ...

  9. sqoop的增量导入(increment import)

    1.import增量导入的官方说明

  10. js导入导出excel

    导入: <html xmlns="http://www.w3.org/1999/xhtml" > <head>      <title>Unti ...