【学习笔记】MySQL和Redis

MySQL

创建数据库
表格处理
修改数据
修改表格的编码集:alter table <表名> charset=<新编码集>;
修改字段的编码集:alter table <表名> modify <字段名> <类型> charset <新编码集>;

import pymysql
import hashlib

db = pymysql.connect(host='localhost', user='root', password='123456',
                     port=3306, charset='utf8', database='python')

cursor = db.cursor()
cursor.execute('select * from stud')
cursor.close()
db.commit()

data = cursor.fetchall()  # 获取所有数据
for info in data:
    print(info)

username = input('please input username: ')
password = input('please input password: ')
h = hashlib.md5()
h.update(password.encode('utf8'))
password = h.hexdigest()

cursor = db.cursor()
# sql注入
# 如果输入用户名为 zhangsan"# , 则mysql语句为 select * from user where name="zhangsan"#" and password="%s"
# cursor.execute('select * from user where name="%s" and password="%s"' % (username, password))

sql = 'select * from user where name=%s and password=%s'
cursor.execute(sql, (username, password))
cursor.close()
db.commit()

db.close()
result = cursor.fetchone()  # 获取一行数据

if not result:
    print('用户名或密码错误!')
else:
    print('Welcome, {}!'.format(username))

redis

import redis

client = redis.Redis()

client.set('name', 'zhangsan')
print(client.get('name'))
client.delete('name')

client.hset('dog', 'name', 'keji')
client.hset('dog', 'age', 3)
client.hset('dog', 'color', 'yellow')
print(client.hkeys("dog"))  # 取hash中所有的key
print(client.hget("dog", "name"))  # 单个取hash的key对应的值
print(client.hmget("dog", "name", "age", "color"))  # 多个取hash的key对应的值
client.hdel("dog", "name", "age", "color")

client.lpush("list1", 11, 22, 33)
print(client.lrange('list1', 0, -1))
client.rpush("list2", 44, 55, 66)  # 在列表的右边,依次添加44,55,66
print(client.llen("list2"))  # 列表长度
print(client.lrange("list2", 0, -1))
while True:
    client.blpop(["list1", "list2"], timeout=2)
    print(client.lrange("list1", 0, -1), client.lrange("list2", 0, -1))

SQL执行顺序

from -> join on -> where -> group by -> having + 聚合函数 -> select -> order by -> limit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值