数据库操作

Python 操作 Mysql 模块的安装

1
2
3
4
5
linux:
    yum install MySQL-python
 
window:
    http://files.cnblogs.com/files/wupeiqi/py-mysql-win.zip

SQL基本使用

1、数据库操作

1
2
3
show databases;
use [databasename];
create database  [name];

2、数据表操作

1
2
3
4
5
6
7
8
9
10
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 "-"
    );
 CREATE TABLE `wb_blog` (
     `id` smallint(8) unsigned NOT NULL,
     `catid` smallint(5) unsigned NOT NULL DEFAULT ',
     `title` varchar(80) NOT NULL DEFAULT '',
     `content` text NOT NULL,
     PRIMARY KEY (`id`),
     UNIQUE KEY `catename` (`catid`)
 ) ; 

创建表wb_blog

3、数据操作

1
2
3
4
5
6
7
insert into students(name,sex,age,tel) values('alex','man',18,'151515151')
 
delete from students where id =2;
 
update students set name = 'sb' where id =1;
 
select * from students

4、其他

1
2
3
主键
外键
左右连接

Python MySQL API

一、插入数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import MySQLdb
  
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')
  
cur = conn.cursor()
  
reCount = cur.execute('insert into UserInfo(Name,Address) values(%s,%s)',('alex','usa'))
# reCount = cur.execute('insert into UserInfo(Name,Address) values(%(id)s, %(name)s)',{'id':12345,'name':'wupeiqi'})
  
conn.commit()
  
cur.close()
conn.close()
  
print reCount
import MySQLdb

conn = MySQLdb.connect(host=',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

MySQLdb

注意:cur.lastrowid

二、删除数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import MySQLdb
 
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')
 
cur = conn.cursor()
 
reCount = cur.execute('delete from UserInfo')
 
conn.commit()
 
cur.close()
conn.close()
 
print reCount

三、修改数据

1
2
3
4
5
6
7
8
9
10
11
12
13
import MySQLdb
 
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',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='1234',db='mydb')
cur = conn.cursor()

reCount = cur.execute('select * from UserInfo')

print cur.fetchone()
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='1234',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]
   

python之数据库操作的更多相关文章

  1. python之数据库操作(sqlite)

    python之数据库操作(sqlite) 不像常见的客户端/服务器结构范例,SQLite引擎不是个程序与之通信的独立进程,而是连接到程序中成为它的一个主要部分.所以主要的通信协议是在编程语言内的直接A ...

  2. python sqlite3 数据库操作

    python sqlite3 数据库操作 SQLite3是python的内置模块,是一款非常小巧的嵌入式开源数据库软件. 1. 导入Python SQLite数据库模块 import sqlite3 ...

  3. Python的数据库操作(Sqlalchemy)

    ORM 全称 Object Relational Mapping, 翻译过来叫对象关系映射.简单的说,ORM 将数据库中的表与面向对象语言中的类建立了一种对应关系.这样,我们要操作数据库,数据库中的表 ...

  4. 10分钟教你Python+MySQL数据库操作

    欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 本文介绍如何利用python来对MySQL数据库进行操作,本文将主要从以下几个方面展开介绍: 1.数据库介绍 2.MySQL数据库安装和设置 ...

  5. [Python]MySQLdb for Python使用指南/Python的数据库操作

    网站就是要和数据库进行交互,否则什么都不用做了...今天我们来看一个叫MySQLdb的库,这个用来和MySQL数据库进行交互.可以从这里获得这个库http://sourceforge.net/proj ...

  6. Python的数据库操作(pymysql)

    使用原生SQL语句进行对数据库操作,可完成数据库表的建立和删除,及数据表内容的增删改查操作等.其可操作性很强,如可以直接使用“show databases”.“show tables”等语句进行表格之 ...

  7. Python的数据库操作

    使用原生SQL语句进行对数据库操作,可完成数据库表的建立和删除,及数据表内容的增删改查操作等.其可操作性很强,如可以直接使用“show databases”.“show tables”等语句进行表格之 ...

  8. Python Django 数据库操作

    1. 建立app 在自己的工程项目目录下输入: python manage.py startapp myapp(你想建立的app名称) 建立一个叫myapp的app 这样,在你的工程项目目录下会出现一 ...

  9. python mysql数据库操作

    一.pymysql 模块安装(本文博客推荐:https://www.cnblogs.com/clschao/articles/10023248.html) pip3 install pymysql 二 ...

随机推荐

  1. JS实现输入框只能输入数字

    键盘下落事件实现输入框只能输入数字: <script type="text/javascript"> // 实现输入框只能输入数字 function ValidateN ...

  2. Azure Application Gateway (1) 入门

    <Windows Azure Platform 系列文章目录> 请读者注意,Azure Application Gateway在ASM模式下,只能通过PowerShell创建 具体可以参考 ...

  3. 让你的网站免费支持 HTTPS 及 Nginx 平滑升级

    为什么要使用 HTTPS ? 首先来说一下 HTTP 与 HTTPS 协议的区别吧,他们的根本区别就是 HTTPS 在 HTTP 协议的基础上加入了 SSL 层,在传输层对网络连接进行加密.简单点说在 ...

  4. 使用、支持、帮助Moon.Orm

    1.关于Moon.Orm的说明 1)任何人和组织都可以免费使用该框架;(赞助者提供长期的技术咨询)  微信微信: 2)5.0之前已经全部开源; 3)5.0标准版本目前对参与者开源(看看下面很简单的), ...

  5. 【原创】HDFS介绍

    一.            HDFS简介 1.    HDFS全称 Hadoop Distributed FileSystem,Hadoop分布式文件系统. Hadoop有一个抽象文件系统的概念,Ha ...

  6. [译] Paxos算法详解

    1. 概述 Paxos算法被用来实现一个容错的分布式系统,一直以来以晦涩难懂著称.这可能是因为该算法最开始使用希腊文表述的.事实上,它是所有分布式算法中最简单易懂的.Paxos算法的本质其实就是一个共 ...

  7. Android 不一样的原生分享

    Android做分享功能百度一下就两种方案,其一是用系统原生的Activity,最终弹出一个对话框,下面这种的还好,像右图的那种就嫌弃了,上面提供的应用也相对杂,还记得有次测试还给鄙人提了个Bug:建 ...

  8. Hibernate框架中Criteria语句

    在Hibernate中有一种查询语句是Criteria查询(QBC查询),今天呢 我们就一个个的详细的跟大家一起探讨Criteria语句的相关知识点 案例前的准备 //插入测试数据,构建数据库 pub ...

  9. ReSharper.8.0.14.856注册码

    ReSharper.8.0.14.856注册码 用户名:ronle 注册码:ZoJzmeVBoAv9Sskw76emgksMMFiLn4NM 网络转载

  10. MyEclipse或者Eclipse内存溢出问题

    对于一些项目,我们经常会遇到内存溢出的问题,这个时候我们就需要进行设置JDK的各个内存的大小了. 如果是运行项目以后出现问题的话,那么主要还是jdk或者是tomcat的内存问题,至于网上说的xxx.i ...