摘要
通过阅读该文章能够了解数据库的命令以及在python中的使用
正文
数据库的命令
创建一个库
CREATE DATABASE Library_management;
创建一个名为Library_management的库
使用数据库
USE Library_management;
使用并进入库Library_management
创建表
CREATE TABLE log(
account VARCHAR(100) ,
password VARCHAR(100)
);
创建表log,在其中添加列名account以及password
增加值
INSERT INTO log VALUES ('yy','123456')
增加account为yy,password为123456的数据。
遍历表格
SELECT * FROM log;
遍历表格log。
python 对数据库的操作
python对mysql数据库操作利用pymysql库进行操作。
##链接数据库
conn = pymysql.connect(host="localhost", user="root", passwd="1234", db="Library_management")
cursor = conn.cursor()
##查找表中的数据
cursor.execute(f'SELECT * FROM log WHERE account="{account_info}"')
data=cursor.fetchall()
##增加表中的数据
cursor.execute(f'INSERT INTO log VALUES ("{account_info}","{password_info}")')
##删除表中的数据
cursor.execute(f'DELETE FROM book WHERE account="{_account}"')
有了以上的命令就能够对一个数据库进行添加数据和读取修改数据了。