API应用程序接口

1.Redis接口

1.1 安装

pip install redis

1.2 操作

import redis 
conn = redis.strictRedis(              // 连接redis//
	host='localhost',                  //默认值,redis一般不进行远程外网连接,一般都为localhost//
	port=6379,						   //默认端口//
	db=0,                             //默认数据库//
	decode_responses=True             //默认为Flase返回值类型为bytes,True返回值类型为str//
	)

python中操作与redis中几乎一致,除了del与python中关键字重名,用delete替换。

res = conn.keys('*')                                     //返回一个列表//
res = conn.set('age', 19)                                //插入单个键值对//
res = conn.get('age')                                    //读取age所对应的值//
res = conn.hset('class',{'zhengrun':'boy'})              //插入单个哈希//
res = conn.hmset('class',{'zhengrun':'boy'})             //插入多个哈希//
res = conn .hgetall('class')                             //单个哈希读取field 和 value//
res = conn.hget('class','zhengrun')                      //单个哈希读取 value//
res = conn .hkeys('class')                               //读取哈希全部field//
res = conn .hvals('class')                               //读取哈希全部value//
res = conn .hlen('class')                                //获取field个数//
res = conn.lpush('list',1,2,3,4,5)                       //插入单个列表,后插入的在最左边,”栈”//
res = conn.rpush(……)                                    //后插入的在最右边,“队列”//
res = conn.lrange('list',0,-1)                           //读取单个列表//
res = conn.llen('list')							   	   	//返回长度//
res = conn.lindex('list',0)                              //返回索引位置元素//
res = conn.lpop('list')									 //左删除//
res = conn.rpop('list')                                  //右删除//
res = conn.lrem('list',count=1,value=3)                  //删除list中数量为1个的值为3的数据,//
res = conn.sadd('tanzhou',1,2,3,4,5,6,7,8,9,11,1)        //插入单个集合,重复的不会插入/,自动忽略//
res = conn.smembers('tanzhou')                           //读取单个集合//
res = conn.srem('tanzhou',1,2)                           //删除指定元素//
res = conn.spop('tanzhou')                               //随机删除元素//
res = conn.zadd('charge',{'math':97,'tiyu':60})          //插入单个有序集合//
res = conn.zrange('charge',0,100)                        //读取单个有序集合//
res = conn.zrangebyscore('charge',0,100)                 //读取单个有序集合//
res = conn.delete('li')                                  //删除单条数据//
res = conn.append('name','tzhou')                        //字符串:在已有的值后面增加内容//
res = conn.ttl('name')                                   //查看过期时间//
res = conn.mset({'zhao':1,'qian':2,'sun':3,'li':4})      //插入多个键值对//
res = conn.mget('zhao','qian','sun','li')                //读取多条键值对//
res = conn.exists('zhao','qian','sun','li','wang')       //返回存在的key的数量,不存在的key不计数//
res = conn.type('zhao')                                  //返回key对应值得类型//

2.mysql接口

pymysql

2.1 安装

pip install pymysql

2.2操作

import pymysql

//新建连接//
db_config = {
	 user='root',
	 password='qwe123',
	 db='test',
	 charset='utf-8'
   	'port'=3306
}
conn = pymysql.connect(**db_config)
	
	
//创建游标//

cursor = conn.cursor(pymysql.cursors.DictCursor)     //以字典形式输出//

//执行sql语句//
try:
	cursor.execute(sql语句)
	res = cursor.fetchall()                 //获取全部//
	res = cursor.fetchone()                 //获取一条//
	res = cursor.fetchmany(3)               //获取多条//
	conn.commit()                           //增删改时需要提交//
except Exception as e:
	print(e)
	conn.rollback()                         //回滚,报错时要回退掉之前的操作,只是在当前会话有效,在其他会话是不生效//

//关闭游标//

cursor.close()

//关闭连接//

conn.close()
    

MySQL官方文档:https://dev.mysql.com/doc/refman/8.0/en/innodb-autocommit-commit-rollback.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值