<tfoot id='QQ7if'></tfoot>

    <i id='QQ7if'><tr id='QQ7if'><dt id='QQ7if'><q id='QQ7if'><span id='QQ7if'><b id='QQ7if'><form id='QQ7if'><ins id='QQ7if'></ins><ul id='QQ7if'></ul><sub id='QQ7if'></sub></form><legend id='QQ7if'></legend><bdo id='QQ7if'><pre id='QQ7if'><center id='QQ7if'></center></pre></bdo></b><th id='QQ7if'></th></span></q></dt></tr></i><div id='QQ7if'><tfoot id='QQ7if'></tfoot><dl id='QQ7if'><fieldset id='QQ7if'></fieldset></dl></div>

  • <legend id='QQ7if'><style id='QQ7if'><dir id='QQ7if'><q id='QQ7if'></q></dir></style></legend>

    <small id='QQ7if'></small><noframes id='QQ7if'>

      • <bdo id='QQ7if'></bdo><ul id='QQ7if'></ul>

        python笔记:mysql、redis操作方法

        在Python3中使用MySQL,需要先安装pymysql模块,可以使用以下命令进行安装:

        <i id='xp6N6'><tr id='xp6N6'><dt id='xp6N6'><q id='xp6N6'><span id='xp6N6'><b id='xp6N6'><form id='xp6N6'><ins id='xp6N6'></ins><ul id='xp6N6'></ul><sub id='xp6N6'></sub></form><legend id='xp6N6'></legend><bdo id='xp6N6'><pre id='xp6N6'><center id='xp6N6'></center></pre></bdo></b><th id='xp6N6'></th></span></q></dt></tr></i><div id='xp6N6'><tfoot id='xp6N6'></tfoot><dl id='xp6N6'><fieldset id='xp6N6'></fieldset></dl></div>
          <bdo id='xp6N6'></bdo><ul id='xp6N6'></ul>

                <tbody id='xp6N6'></tbody>
            1. <small id='xp6N6'></small><noframes id='xp6N6'>

              <tfoot id='xp6N6'></tfoot>
                <legend id='xp6N6'><style id='xp6N6'><dir id='xp6N6'><q id='xp6N6'></q></dir></style></legend>
                • Python笔记:MySQL、Redis操作方法

                  MySQL的常用模块

                  在Python3中使用MySQL,需要先安装pymysql模块,可以使用以下命令进行安装:

                  pip3 install pymysql
                  

                  需要连接数据库时,可以使用以下代码:

                  import pymysql
                  
                  # 打开数据库连接
                  db = pymysql.connect(host='localhost', user='root', password='password', database='test', charset='utf8')
                  
                  # 使用 cursor() 方法创建一个游标对象 cursor
                  cursor = db.cursor()
                  
                  # 使用 execute() 方法执行 SQL 查询
                  cursor.execute("SELECT VERSION()")
                  
                  # 使用 fetchone() 方法获取单条数据.
                  data = cursor.fetchone()
                  
                  print("Database version : %s " % data)
                  
                  # 关闭数据库连接
                  db.close()
                  

                  插入数据

                  使用以下代码可以向MySQL数据库中插入数据:

                  import pymysql
                  
                  # 打开数据库连接
                  db = pymysql.connect(host='localhost', user='root', password='password', database='test', charset='utf8')
                  
                  # 使用 cursor() 方法创建一个游标对象 cursor
                  cursor = db.cursor()
                  
                  # SQL 插入语句
                  sql = "INSERT INTO EMPLOYEE(FIRST_NAME,\
                         LAST_NAME, AGE, SEX, INCOME) \
                         VALUES ('%s', '%s', '%d', '%c', '%d' )" % \
                         ('Mac', 'Mohan', 20, 'M', 2000)
                  
                  try:
                     # 执行sql语句
                     cursor.execute(sql)
                     # 提交到数据库执行
                     db.commit()
                  except:
                     # 如果发生错误则回滚
                     db.rollback()
                  
                  # 关闭数据库连接
                  db.close()
                  

                  查询数据

                  使用以下代码可以从MySQL数据库中查询数据:

                  import pymysql
                  
                  # 打开数据库连接
                  db = pymysql.connect(host='localhost', user='root', password='password', database='test', charset='utf8')
                  
                  # 使用 cursor() 方法创建一个游标对象 cursor
                  cursor = db.cursor()
                  
                  # SQL 查询语句
                  sql = "SELECT * FROM EMPLOYEE \
                          WHERE INCOME > '%d'" % (1000)
                  
                  try:
                     # 执行SQL语句
                     cursor.execute(sql)
                     # 获取所有记录列表
                     results = cursor.fetchall()
                     for row in results:
                        fname = row[0]
                        lname = row[1]
                        age = row[2]
                        sex = row[3]
                        income = row[4]
                        # 打印结果
                        print("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
                               (fname, lname, age, sex, income ))
                  except:
                     print("Error: unable to fetch data")
                  
                  # 关闭数据库连接
                  db.close()
                  

                  Redis的常用模块

                  在Python3中使用Redis,需要先安装redis模块,可以使用以下命令进行安装:

                  pip3 install redis
                  

                  需要连接Redis时,可以使用以下代码:

                  import redis
                  
                  # 连接redis
                  r = redis.Redis(host='localhost', port=6379, db=0)
                  
                  # 写入数据
                  r.set('name', 'Redis')
                  
                  # 读取数据
                  print(r.get('name'))
                  
                  # 删除数据
                  r.delete('name')
                  

                  递增操作

                  Redis支持对键进行递增操作,可以使用以下代码:

                  import redis
                  
                  # 连接redis
                  r = redis.Redis(host='localhost', port=6379, db=0)
                  
                  # 写入数据
                  r.set('count', 1)
                  
                  # 递增
                  r.incr('count')
                  
                  # 读取数据
                  print(r.get('count'))
                  
                  # 递减
                  r.decr('count')
                  
                  # 读取数据
                  print(r.get('count'))
                  

                  数据库连接池

                  使用连接池可以减少连接数据库的开销,可以使用以下代码:

                  import redis
                  from redis import ConnectionPool
                  
                  # 创建连接池
                  pool = ConnectionPool(host='localhost', port=6379, db=0)
                  
                  # 获取连接
                  r = redis.Redis(connection_pool=pool)
                  
                  # 写入数据
                  r.set('name', 'Redis')
                  
                  # 读取数据
                  print(r.get('name'))
                  
                  # 删除数据
                  r.delete('name')
                  

                  以上是MySQL和Redis在Python中常用的操作方法,更多详细信息可以参考官方文档。

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  下面是针对PostgreSQL中的权限问题的完整攻略。
                  MySQL是一种流行的关系型数据库系统,它提供了多种时间类型和模式,用于存储和处理时间数据。本文将详细介绍MySQL时间类型和模式的详细攻略。
                  首先在官网下载CentOS7镜像,并在VMware虚拟机中新建一台CentOS7虚拟机,将镜像挂载到虚拟机中并启动。
                  首先,当我们使用Spring Boot开发项目时,可能会遇到Error starting ApplicationContext错误,一般这种错误是由于配置文件、依赖包或者代码逻辑等原因引起的。下面我将提供一条包含两条详细示例说明的完整攻略,用来解决上述问题。
                  下面我将详细讲解如何为PostgreSQL数据库中的用户授予权限和撤销权限,包括两个实例。
                  MySQL中出现lock wait timeout exceeded问题的原因是由于两个或多个事物同时请求相同的资源造成的,并且在某一时刻至少一个事务无法获取资源,超过了MySQL默认的等待时间,从而导致事务失败。这种问题的出现会极大地影响数据库的性能和并发能力。
                • <tfoot id='HCWTu'></tfoot>

                  <small id='HCWTu'></small><noframes id='HCWTu'>

                    <bdo id='HCWTu'></bdo><ul id='HCWTu'></ul>
                    <i id='HCWTu'><tr id='HCWTu'><dt id='HCWTu'><q id='HCWTu'><span id='HCWTu'><b id='HCWTu'><form id='HCWTu'><ins id='HCWTu'></ins><ul id='HCWTu'></ul><sub id='HCWTu'></sub></form><legend id='HCWTu'></legend><bdo id='HCWTu'><pre id='HCWTu'><center id='HCWTu'></center></pre></bdo></b><th id='HCWTu'></th></span></q></dt></tr></i><div id='HCWTu'><tfoot id='HCWTu'></tfoot><dl id='HCWTu'><fieldset id='HCWTu'></fieldset></dl></div>
                      <tbody id='HCWTu'></tbody>
                    <legend id='HCWTu'><style id='HCWTu'><dir id='HCWTu'><q id='HCWTu'></q></dir></style></legend>