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() #关闭连接
  • 相关阅读:
    [考试]20151017数据结构
    [考试]20151016动态规划
    [考试]20151015分治
    [BZOJ1501/NOI2005]智慧珠游戏
    [BZOJ3139/HNOI2013]比赛
    [考试]20151013搜索
    BZOJ3082: Graph2
    BZOJ4690: Never Wait for Weights
    BZOJ4668: 冷战
    BZOJ3262: 陌上花开
  • 原文地址:https://www.cnblogs.com/louis-w/p/8353308.html
Copyright © 2011-2022 走看看