zoukankan      html  css  js  c++  java
  • Python基础学习六 操作MySQL

    python操作数据库,需要先安装模块

    1、下载MySQL、Redis模块 

    2、解压后,在当前目录执行 python setup.py install

    3、或是pycharm直接选择安装

     1 import pymysql
     2 
     3 conn = pymysql.connect(host='192.168.213.134',
     4                 user = 'root',
     5                 passwd= '123456',
     6                 port = 3306,
     7                 db='dangdang',
     8                 charset='utf8')
     9 
    10 #建立游标,游标你就认为是仓库管理员,指定curson的类型:字典类型
    11 cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
    12 
    13 cur.execute('select * from d_book where id<4;') #执行sql语句
    14 
    15 res1 = cur.fetchall() #获取sql语句执行的结果.它把结果放到一个元组里,每一条数据也是一条元组
    16 print('fetchall:',res1)
    17 
    18 
    19 cur.scroll(0,mode='absolute') #移动游标,到最前面
    20 # cur.scroll(3,mode='relative') #移动游标,相对于当前位置的
    21 res2 = cur.fetchone() #只获取一条结果
    22 print('
    fetchone:',res2)
    23 print('查询内容:',res2['publishing'])
    24 
    25 cur.close() #关闭游标
    26 conn.close() #关闭连接
  • 相关阅读:
    leetcode-滑动窗口
    leetcode刷题-双指针
    nlp
    机器学习
    tf-idf算法
    RNN和LSTM的理解
    DDD落地实践-战术实现心得
    DDD落地实践-战略设计心得
    测试平台系列(66) 数据驱动之基础Model
    Python小知识之对象的比较
  • 原文地址:https://www.cnblogs.com/louis-w/p/8353308.html
Copyright © 2011-2022 走看看