mySQL 数据库
-
创建与删除数据库
create database book;
drop database book;
-
创建与删除表
create table detail_book( id int primary key auto_increment, bookname char(50) not null, price int not null);
drop table detail_book;
-
数据库的简单操作
增 insert into detail_book(bookname,price) values (‘共产党宣言’,20);
删 delete from detail_book where id = 2;
改 update detail_book set bookname = ‘fdsa’ where id = 1;
查 select * from detail_book;
-
在python 中的使用
import pymysql # mysql 测试 # 连接数据库 db = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='111111‘, database='book', charset='utf8' ) # 创建游标 cursor = db.cursor() try: # 执行sql 语句 cursor.execute('insert into detail_book (bookname,price) values (%s,%s)',('水浒传',60)) # 执行后 提交事务 db.commit() except Exception as e: # 如有异常进行事务回滚 print(e) db.rollback() # 游标的常用方法: # execute() 执行sql 语句 # executemany() 执行多条sql 语句 # fetchone() 查询表记录,并将指针放在下一条记录的开头 # fetchmany() 返回所有的表记录 # fetchall() 返回表记录的迭代器 # close() 关闭游标