python3使用mysql的基础

前段时间用python3写了一个前端web页面,实现从mysql数据库提取数据并做一个展示,东西都很基础,记录一下怕以后忘记。

python3的前期准备工作

python3 跟python2 比,在mysql使用上是不一样的,python3 没有了mysqldb模块,不能直接从Pycharm下载,代之的是PyMySQL和 mysqlclient,我用的是mysqlcliet,下载这个模块之后就可以调用MySQLdb了,所以还是很方便的。

mysql基础语句

python中:
for example:
import MySQLdb
conn = MySQLdb.connect(host=“IP地址”,user="###",passwd="###",db="###",port=11306,charset=“utf8”) #建立连接
cur = conn.cursor() #游标

cur.execute(“select * from statis”) #执行搜索
cur.execute(“select * from statis where id=1”)
line_first = cur.fetchone()
print (line_first)

最后一定要关闭数据库和游标:
cur.close()
conn.close()

cur.execute("insert into info 。。。‘’)#插入数据
conn.commit()

在终端:

mysql -u root -p #连接
show databases;
use test(数据库名);
show tables;
desc info(表名);

create table users(id int(2) not null primary key auto_increment,username varchar(40),password text,email text)default charset=utf8;

搜索:
select * from …
分组搜索用group by…
在这里插入图片描述

Tornado

最后说一下,web框架我是用Tornado 写的,简洁实用,不做过多介绍了。