python日记——数据库操作
来源:互联网 发布:华军软件下载中心 编辑:程序博客网 时间:2024/06/05 13:31
在说用pyhton实现之前先科普一下数据库常用的命令
1.终端启动MySQL:/etc/init.d/mysql start;(stop ,restart。)
2.登录MySQL:mysql -uroot -p (用root账户登录),然后输入密码;
3.查看所有的数据库名字:show databases;
4.选择一个数据库操作: use database_name;
5.查看当前数据库下所有的表名:show tables;
6.创建一个数据库:create database database_name;
7.删除一个数据库:drop database database_name;
8.创建一个表: create table mytest( uid int(4) primary key auto_increment, uname varchar(20) not null);
9.删除一个表: drop table mytest;
10.SQL插入语句:insert into table_name(col1,col2) values(value1,value2);//datetime要用双引号引起来
11.SQL更新语句:update table_name set col1='value1',col2='value2' where where_definition;
12.SQL查询语句:select * from table_name where.......(最复杂的语句)//字符串类型要用单引号
13.SQL删除语句:delete from table_name where...
14.增加表结构的字段:alert table table_name add column field1 date ,add column field2 time...
15.删除表结构的字段:alert table table_name drop field1;
16.查看表的结构:show columns from table_name;
17.limit 的使用:select * from table_name limit 3;//每页只显示3行
select * from table_name limit 3,4 //从查询结果的第三个开始,显示四项结果。
此处可很好的用来作分页处理。
18.对查询结果进行排序: select * from table_name order by field1,orderby field2;多重排序
19.退出MySQL:exit;
接下来看pyhton代码
增:
import MySQLdbcon= MySQLdb.connect(host='localhost',user='root',passwd='root',db='dbname')cursor =con.cursor()sql ="insert into table_name(col1,col2) values(value1,value2)"cursor.execute(sql)con.commit()cursor.close()con.close()
删:
import MySQLdbcon= MySQLdb.connect(host='localhost',user='root',passwd='root',db='dbname')cursor =con.cursor()sql ="delete from table_name where col1=value1"cursor.execute(sql)con.commit()cursor.close()con.close()
import MySQLdbcon= MySQLdb.connect(host='localhost',user='root',passwd='root',db='dbname')cursor =con.cursor()sql ="select * from table_name where col1=value1"#不加where和后面的东西就是查询表内的全部数据cursor.execute(sql)for row in cursor.fetchall(): print row#row是数组形式,对应每一个字段cursor.close()con.close()
改:
import MySQLdbcon= MySQLdb.connect(host='localhost',user='root',passwd='root',db='dbname')cursor =con.cursor()sql ="update table_name set col1='value1',col2='value2' where where_definition"cursor.execute(sql)con.commit()cursor.close()con.close()
#如果数据库语句中含变量要转化为字符串然后再和其他拼在一起#转化为字符串可用str(value1)sql ="insert into table_name(col1,col2) values("+value1+","+value2+")"
注意:增删改都要commit
其实python的数据库处理都是大同小异,只要明白语法就行了,如果是字符串的话别忘了要用引号哦。
- python日记——数据库操作
- PyMySQL——python操作数据库
- Python Web 6 —— Python操作MongoDB数据库
- python日记——使用django心得
- python学习日记——1
- Python日记——model的使用
- JSP学习日记(三) JDBC数据库操作
- Python基础知识——对mysql数据库的操作
- Python操作Mysql数据库
- python的数据库操作
- python操作数据库
- python SQlite数据库操作
- python操作MySQL数据库
- python操作MySQL数据库
- python操作数据库
- Python 数据库操作
- python操作MySQL数据库
- python操作MySQL数据库
- Retrofit2.0进行图片上传(后端Spring MVC)
- C语言,函数指针与指针函数
- google更新5.0以后,用eclipse创建工程时出现的找不到styles.xml中的资源的问题
- ROS简明教材目录
- 【一天一道LeetCode】#5 Longest Palindromic Substring
- python日记——数据库操作
- display属性
- 1. Two Sum
- [BZOJ1787][AHOI2008]Meet 紧急集合(LCA)
- HDFS 与 GFS 的设计差异
- 常用的文件和目录操作命令(转)
- 40. Combination Sum II 回溯算法
- AutoIt3 是Windows自动安装脚本语言
- 京东咚咚架构演进