MySQL and Python's MySQLdb
来源:互联网 发布:mac系统壁纸路径 编辑:程序博客网 时间:2024/06/06 02:15
#本文主要介绍MySQLdb和MySQL常见用法
##MySQL常见用法
创建数据库 create database if not exists student;
删除数据库 drop database if exists student;
查看所有数据库 show databases;
选择数据库 use student;
创建数据表 create table tbl_score(
rl_sName varchar(20) not null,
rl_uScore int(3) not null,
primary key(rl_sName)
);
删除数据表 drop table tbl_score;
显示所有数据表 show tables;
显示表结构 desc tbl_score;
显示创建表过程(语法) show create table tbl_score;
增加一条数据 insert into tbl_score(rl_sName,rl_uScore) values("zuiaiqun",100);
显示表内容 select * from tbl_score;
修改一条数据 update tbl_score set rl_uScore=99 where rl_sName="zuiaiqun";
删除某一条数据 delete from tbl_score where rl_sName="zuiaiqun";
修改表结构 alter table tbl_score add rl_uSex int(3) not null after rl_uScore;
---
比较容易弄错的是“修改表结构”和“增加一条数据”这两个指令。。
##MySQLdb用法
保证你已经安装了MySQLdb模块,文件可以在此处下载http://sourceforge.net/projects/mysql-python/
使用例子:
#encoding:utf-8import MySQLdbdef TestMySQLdb():#连接MySQLconn=MySQLdb.connect(host="localhost",user="root",passwd="zuiaiqun",db="student")cursor=conn.cursor()sSQL="drop table tbl_role"try:cursor.execute(sSQL)except:pass#创建数据表sSQL='''create table tbl_role(rl_uID int(10) not null,rl_sName varchar(20) not null,primary key(rl_uID))'''try:cursor.execute(sSQL)except:pass#插入数据sSQL="insert into tbl_role(rl_uID,rl_sName) values(%s,\"%s\")"for uid in xrange(1,20):sName="zuiaiqun%d"%uidsExecute=sSQL%(uid,sName)cursor.execute(sExecute)sSQL="select * from tbl_role"cursor.execute(sSQL)tRes=cursor.fetchall()for tVal in tRes:#tVal与select相关,由于是select *,所以tVal=(rl_uID,rl_sName)print tValsSQL="select rl_uID from tbl_role"cursor.execute(sSQL)tRes=cursor.fetchall()for tVal in tRes:#tVal与select相关,由于是select rl_uID,所以tVal=(rl_uID,)print tValconn.commit()#测试发现如果有修改表的数据(insert into),得执行commit,否则无法成功修改数据库cursor.close()conn.close()if __name__=="__main__":TestMySQLdb()
##结果:
- MySQL and Python's MySQLdb
- mysql-python MySQLdb
- Python连接MYSQL --- MySQLdb
- Python MySQL(MySQLdb)
- python mysql MySQLdb
- python之mysql连接 MySQLdb
- Python使用MySQLdb操作MySQL
- python mysql MySQLdb 存储文件
- python操作MySQL数据库(MySQLdb
- Linux + python +mysqlDB + mysql + innoDB
- python通过MySQLdb操纵mysql
- MySQLdb:Python 操作 MySQL 数据库
- ubuntu 安装MySQL-python(MySQLdb)
- python-mysql安装mysqldb模块
- python 通过mysqldb 操作mysql数据库
- python中利用MySQLdb操作mysql(二)
- python中使用MySQLdb连接mysql
- Python访问Mysql安装MySQLdb【Fedora】
- 黑马程序员05——面向对象2
- POJ 1038 状压DP
- UVA 1347(POJ 2677) Tour(双调欧几里得旅行商问题)
- Unix:关于淘宝开源tsar(src)的makefile
- shell基础五:输入和输出/正则表达式
- MySQL and Python's MySQLdb
- 【最短路】 HDOJ 1317 XYZZY
- HBase全分布式环境搭建
- poj3180 The Cow Prom(强连通)
- 简单的大数模板
- ZOJ 1365
- 安卓UI横竖转换注意事项
- 黑马程序员06——内部类、包
- leetcode Path Sum && Path Sum ||