1. 修改mysql配置文件

修改bind-address=0.0.0.0(允许通过远程网络连接)

2. 修改redis配置文件

修改bind-address=0.0.0.0(允许通过远程网络连接),设置密码qwe123

3. 下载访问包pymysql和redis

4. 设置端口转发mysql和redis

5. 导入包pymysql连接mysql

import pymysql
mysql_connect_dict={
'host':'127.0.0.1',
'port':3333,
'user':'jianeng',
'password':'qwe123',
'db':'info',
'charset':'utf8'
}
# 连接数据库
conn = pymysql.connect(**mysql_connect_dict)
# 指定以dict形式返回,默认以元祖形式
#conn = pymysql.connect(**mysql_connect_dict,cursorclass=pymysql.cursors.DictCursor)
print(conn)

6. 访问mysql

1. 查询记录

# 创建游标
cursor = conn.cursor()
# sql查询语句
sql = "show databases"
# 执行sql,得到行数
row = cursor.execute(sql);
print('%s条数据'%row)
# 返回一条记录(元祖形式)
one = cursor.fetchone();
print(one)
# 返回多条记录(元祖形式)
many = cursor.fetchmany(3)
print(many)
# 返回所有记录(元祖形式)
all = cursor.fetchall()
print(all)
#循环输出
for al in all:
print(*al)

打印结果

6条数据
('information_schema',)
(('info',), ('mydb',), ('mysql',))
(('performance_schema',), ('sys',))
#循环结果
performance_schema
sys

2. 删除、创建表

   dr_table ='drop table `user`'
# 删除表
cursor.execute(dr_table) cr_table ='''create table if not exists user(
id int primary key auto_increment,
username varchar(20) not null,
password varchar(20) not null
)
'''
# 创建表
cursor.execute(cr_table)

3. 插入记录

   # 插入数据
insertsql ='insert into user (username, password) VALUES (%s,%s)'
cursor.execute(insertsql,('zhangsan','123'))
# 插入多条(元祖形式)
cursor.executemany(insertsql,[('王五','qwq'),('赵四','123'),('千8','123')])
# 提交数据
conn.commit(); # sql查询语句
selectsql = "select * from user "
# 执行sql,得到行数
row = cursor.execute(selectsql);
print('返回%s条数据'%row)
# 返回所有记录(元祖形式)
select_all = cursor.fetchall();
print("select=",select_all)

打印结果

   #返回4条数据
select= ((1, 'zhangsan', '123'), (2, '王五', 'qwq'), (3, '赵四', '123'), (4, '千8', '123'))

4. 修改记录

   # 更新数据
updatesql='update user set username = %s where id=%s'
cursor.execute(updatesql,('张三','2'))
# 更新多条(元祖形式)
l = []
for x in range(1, 4):
l.append(('李%s'%x,str(x)))
cursor.executemany(updatesql,l)
# 提交数据
conn.commit(); # 执行sql,得到行数
row = cursor.execute(selectsql);
print('返回%s条数据'%row)
# 返回所有记录(元祖形式)
select_all = cursor.fetchall();
print("select=",select_all)

打印结果

#返回4条数据
select= ((1, '李1', '123'), (2, '李2', 'qwq'), (3, '李3', '123'), (4, '千8', '123'))

5. 删除记录

# 删除语句
deletesql='delete from user where id=%s'
cursor.execute(deletesql, 1)
# 删除多条
cursor.executemany(deletesql,[(2,),(3,)])
# 提交数据
conn.commit()

打印结果

#返回1条数据
select= ((4, '千8', '123'),)

7. 访问redis

import redis
import sys
import time
# 得到默认编码
print(sys.getdefaultencoding())
# 连接redis
re = redis.Redis(host='127.0.0.1', password='qwe123',port=5555) # 设置name值
re.set('name',15)
print(type(re.get('name')))#byte类型(utf8格式16进制字节码)
if isinstance(re.get('name'), bytes):
# 字节码转换为字符串
print(re.get('name').decode('utf8')) re.set('name','祖国')
# decode默认为utf8格式解码
print(re.get('name').decode()) # 设置过期时间为3s
re.set('name','祖国',ex=3)
time.sleep(3)
#打印过期后ttl
print(re.ttl('name'))
# 设置多个属性
re.mset(name='佳能',age='18')
print(re.mget('name','age')) # 设置递增
re.incr('age')
print(re.get('age'))
re.incr('age',10)
print(re.get('age'))
# 删除序列的值c
re.lrem('test_list','c',0)
# 设置hash 值
re.hmset('userkey',{'name':'jianeng','age':'18'})
print(re.hgetall('userkey'))

打印结果

utf-8
<class 'bytes'>
15
祖国
None
[b'\xe4\xbd\xb3\xe8\x83\xbd', b'18']
b'19'
b'29'
{b'name': b'jianeng', b'pwd': b'123', b'age': b'18'}

redis终端输出中文

python访问mysql和redis的更多相关文章

  1. 06 python操作MySQL和redis(进阶)

    python操作mysql.redis 阶段一.mysql事务 主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息, ...

  2. Python访问MySQL(1):初步使用PyMySQL包

    Windows 10家庭中文版,MySQL 5.7.20 for Win 64,Python 3.6.4,PyMySQL 0.8.1,2018-05-08 ---- 使用Python访问MySQL数据 ...

  3. python访问mysql

    1,下载mysql-connector-python-2.0.4  pythoin访问mysql需要有客户端,这个就是连接mysql的库 解压后如下图: 双击lib 以windows为例 把mysql ...

  4. 利用Python访问Mysql数据库

    首先要明确一点,我们在Python中需要通过第三方库才能访问Mysql. 有这样几种方式:Mysql-python(即MySQLdb).pymysql.mysql-connector.Mysql-py ...

  5. Python访问MySQL数据库并实现其增删改查功能

    概述:对于访问MySQL数据库的操作,我想大家也都有一些了解.不过,因为最近在学习Python,以下就用Python来实现它.其中包括创建数据库和数据表.插入记录.删除记录.修改记录数据.查询数据.删 ...

  6. python学习道路(day12note)(mysql操作,python链接mysql,redis)

    1,针对mysql操作 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); 设置密码 update user set password ...

  7. python 数据库mysql、redis及发送邮件

    python 关系型数据库链接使用--mysql import pymysql # 引用mysql模块 # 创建连接,指定数据库的ip地址,账号.密码.端口号.要操作的数据库.字符集coon = py ...

  8. Python访问MySQL数据库

    #encoding: utf-8 import mysql.connector __author__ = 'Administrator' config={'host':'127.0.0.1',#默认1 ...

  9. 【数据库】python访问mysql

    import MySQLdb 所有的数据库遵循相同的python database API 需要建立connection对象连接数据库,之后建立cursor对象处理数据. conn = MySQLdb ...

随机推荐

  1. String,StringBuffer,StringBuilder的区别

    数据结构: 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合. 比如数据库就是对硬盘中的数据进行有规则的管理,可以进行增删改查工作,而良好的数据结构可以优化这些操作, 也许大家会想这些和St ...

  2. Sourcetree的安装与使用

    1 安装遇到的问题 https://segmentfault.com/q/1010000007643870 解决该问题的方法: http://www.jianshu.com/p/3478e2a214a ...

  3. linux集群架构

    Linux集群架构   根据功能划分为两大类:高可用和负载均衡 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务 实现高可用的开源软件有:heart ...

  4. Mysql(二):库操作

    一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MyS ...

  5. python实现三级菜单

    一.要求: 1.一开始打印出所有省份和提示 2.用户输入省份以此查询城市 3.在按照输出的城市名提示用户输入,最后输出用户所查询的区县名 4.随时输入"back"可以返回上一级菜单 ...

  6. flask中jinjia2模板引擎使用详解1

    在之前的文章中我们介绍过flask调用jinja2模板的基本使用,这次我们来说一下jinjia2模板的使用 Jinja2 在其是一个 Python 2.4 库之前,被设计 为是灵活.快速和安全的. 模 ...

  7. logback KafkaAppender 写入Kafka队列,集中日志输出.

    为了减少应用服务器对磁盘的读写,以及可以集中日志在一台机器上,方便使用ELK收集日志信息,所以考虑做一个jar包,让应用集中输出日志 网上搜了一圈,只发现有人写了个程序在github 地址:https ...

  8. Android虚拟机安装

    由于虫师那边的源估计到期了,我又找了一波. 打开SDK Manager.exe, 就在安卓目录下. 点击Tools--Options进入配置页面 mirrors.neusoft.edu.cn 配置如下 ...

  9. 校验Linux程序是否被黑客修改

    一个黑客突破你的层层防御后,修改你的程序或者覆盖了你的工具时.确定一个已安装程序的所有文件,有没有被修改过的途径之一就是使用RPM包校验功能 如果图片排版有任何错误,欢迎访问我的简书www.jians ...

  10. 转载 git Unknown SSL protocol error in connection to github.com:443

    1.执行命令:git pull –progress –no-rebase -v "origin",报错,如图1 fatal: unable to access 'https://g ...