txt文件和excel文件可以修改数据,读取数不方便
所以我们选择专业的软件:数据库管理系统.
数据库管理系统:mysql/ oracle / sqlserver /db2 / access
下面以mysql为例
我们平时开发系统时
但是表中获得数据一般是通过程序实现增删改查的。
表名:admin
列:id,整型,自增,主键
username 字符串 不为空
password 字符串 不为空
mobile 字符串 不为空
create table admin(
id int not null auto_increment primary key,
username varchar(16) not null,
password char(64) not null,
mobile char(11) not null
)default charset=utf8;
pip3 install pymysql
import pymysql
# 连接mysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="root", charset='utf8', db='bkys')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# # 发送指令
# cursor.execute("insert into admin(username,password,mobile) values('bkys','root123','55555555555')")
# conn.commit()
# # 发送指令(不要用字符串格式化做sql的拼接,安全隐患sql注入)
# sql = "insert into admin(username,password,mobile) values(%s,%s,%s)"
# cursor.execute(sql, ["zhang", "zhang123", "99999999999"])
# conn.commit()
sql = "insert into admin(username,password,mobile) values(%(n1)s,%(n2)s,%(n3)s)"
cursor.execute(sql, {"n1": "zh", "n2": "zhang123456", "n3": "99999999"})
conn.commit()
# 关闭
cursor.close()
conn.close()
import pymysql
while True:
user = input("用户名:")
if user.upper() == "EXIT":
break
pwd = input("密码:")
mobile = input("手机号:")
# 连接mysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="root", charset='utf8', db='bkys')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 发送指令(不要用字符串格式化做sql的拼接,安全隐患sql注入)
sql = "insert into admin(username,password,mobile) values(%s,%s,%s)"
cursor.execute(sql, [user, pwd, mobile])
conn.commit()
# 关闭
cursor.close()
conn.close()
import pymysql
# 连接mysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="root", charset='utf8', db='bkys')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 发送指令(不要用字符串格式化做sql的拼接,安全隐患sql注入)
cursor.execute("select * from admin")
data_list = cursor.fetchall()
# print(data_list)
for row_dict in data_list:
print(row_dict)
# 关闭
cursor.close()
conn.close()
import pymysql
# 连接mysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="root", charset='utf8', db='bkys')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 发送指令(不要用字符串格式化做sql的拼接,安全隐患sql注入)
cursor.execute("select * from admin where id > %s", [2, ])
data_list = cursor.fetchone()
print(data_list)
# 关闭
cursor.close()
conn.close()
import pymysql
# 连接mysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="root", charset='utf8', db='bkys')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 发送指令(不要用字符串格式化做sql的拼接,安全隐患sql注入)
cursor.execute("delete from admin where id = %s", [3])
conn.commit()
# 关闭
cursor.close()
conn.close()
import pymysql
# 连接mysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="root", charset='utf8', db='bkys')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 发送指令(不要用字符串格式化做sql的拼接,安全隐患sql注入)
cursor.execute("update admin set mobile=%s where id = %s", ["15431",4, ])
conn.commit()
# 关闭
cursor.close()
conn.close()