1.基本用法——建立链接,获取游标,执行sql语句,关闭

  • 建立远程链接账号和权限
  • mysql> grant all on *.* to 'root'@'%' identified by '';
    Query OK, rows affected, warning (0.40 sec) mysql> flush privileges;
    Query OK, rows affected (0.23 sec)
  • #-*- coding:utf-8 -*-
    import pymysql
    user = input("用户名:").strip()
    pwd = input("密码:").strip()
    #建立链接
    conn = pymysql.connect(
    host = "192.168.110.1",
    port = 3306,
    user = "root",
    password = "",
    db = "db1",
    charset = "utf8"
    ) #拿游标
    cursor = conn.cursor() #执行sql
    sql = 'select * from user_info where name = %s and pwd = %s'
    print(sql)
    rows = cursor.execute(sql,(user,pwd))
    #关闭
    cursor.close()
    conn.close() if rows:
    print("登录成功")
    else:
    print("登录失败")

    示例

2.增删改

  • #-*- coding:utf-8 -*-
    import pymysql
    #建立链接
    conn = pymysql.connect(
    host = "192.168.110.1",
    port = 3306,
    user = "root",
    password = "",
    db = "db1",
    charset = "utf8"
    ) #拿游标
    cursor = conn.cursor() #执行sql
    ###########增############
    # sql = 'insert into user_info(name,pwd) values (%s,%s)'
    # #插入一条记录
    # rows = cursor.execute(sql,('xxx',123))
    # conn.commit() # #插入多条记录
    # rows = cursor.executemany(sql,[('xyy','ba'),('yxy','abc'),('yyy','dhdf')])
    # conn.commit()
    # print(rows) ###########删############
    # sql = "delete from user_info where id = %s ;"
    # rows = cursor.execute(sql,(3,))
    # conn.commit()
    # print(rows) ###########改############ sql = 'update user_info set pwd = %s where name = "egon4" '
    rows = cursor.execute(sql,'aaa')
    conn.commit()
    print(rows)
    #关闭
    cursor.close()
    conn.close()

    示例

3.查

  • #-*- coding:utf-8 -*-
    import pymysql
    #建立链接
    conn = pymysql.connect(
    host = "192.168.110.1",
    port = 3306,
    user = "root",
    password = "",
    db = "db1",
    charset = "utf8"
    ) #拿游标
    # cursor = conn.cursor()
    cursor = conn.cursor(pymysql.cursors.DictCursor)#以字典形式显示
    #执行sql
    ###########查############
    sql = 'select * from user_info;'
    rows = cursor.execute(sql)
    # print(rows)
    #一次取一个
    # print(cursor.fetchone()) #打印一条记录
    # print(cursor.fetchone())
    # print(cursor.fetchone())
    # print(cursor.fetchone())
    # print(cursor.fetchone())
    # print(cursor.fetchone())
    # print(cursor.fetchone()) # print(cursor.fetchmany(2))#一次取多个
    # print(cursor.fetchall())#取所有 cursor.scroll(3,mode='absolute') # 相对绝对位置移动
    # cursor.scroll(3,mode='relative') # 相对当前位置移动
    print(cursor.fetchone())
    cursor.scroll(1,mode='relative') # 相对当前位置移动
    print(cursor.fetchone())
    #关闭
    cursor.close()
    conn.close()

    示例——查

4.获取插入的最后一条数据的自增ID

  • #-*- coding:utf-8 -*-
    import pymysql
    #建立链接
    conn = pymysql.connect(
    host = "192.168.110.1",
    port = 3306,
    user = "root",
    password = "",
    db = "db1",
    charset = "utf8"
    ) #拿游标
    cursor = conn.cursor() #执行sql
    ###########增############
    sql = 'insert into user_info(name,pwd) values (%s,%s)'
    rows = cursor.executemany(sql,[('xyyx','ba'),('yxyx','abc'),('yyyx','dhdf')])
    print(cursor.lastrowid)#在插入语句后查询
    conn.commit() #关闭
    cursor.close()
    conn.close()

数据库——pymysql模块的使用(13)的更多相关文章

  1. 第二百七十九节,MySQL数据库-pymysql模块操作数据库

    MySQL数据库-pymysql模块操作数据库 pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数使用方式: 模块名称.connec ...

  2. MySQL数据库-pymysql模块操作数据库

    pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数使用方式: 模块名称.connect() 参数: host=数据库ip port= ...

  3. 使用python连接mysql数据库——pymysql模块的使用

    安装pymysql pip install pymysql 使用pymysql 使用数据查询语句 查询一条数据fetchone() from pymysql import * conn = conne ...

  4. 数据库入门-pymysql模块的使用

    一.pymysql模块安装 由于本人的Python版本为python3.7,所以用pymysql来连接数据库(mysqldb不支持python3.x) 方法一: #在cmd输入 pip3 instal ...

  5. 05 数据库入门学习-正则表达式、用户管理、pymysql模块

    一.正则表达式 正则表达式用于模糊查询,模糊查询已经讲过了 like 仅支持 % 和 _ 远没有正则表达式灵活当然绝大多数情况下 like足够使用 #语法 select *from table whe ...

  6. pymysql模块使用---Python连接MySQL数据库

    pymysql模块使用---Python连接MySQL数据库 浏览目录 pymysql介绍 连接数据库 execute( ) 之 sql 注入 增删改查操作 进阶用法 一.pymysql介绍 1.介绍 ...

  7. Python连接MySQL数据库之pymysql模块使用

    安装PyMySQL pip install pymysql PyMySQL介绍 PyMySQL是在python3.x版本中用于连接MySQL服务器的一个库,2中则使用mysqldb. Django中也 ...

  8. Python连接MySQL数据库之pymysql模块

    pymysql 在python3.x 中用于连接MySQL服务器的一个库:Python2中则使用mysqldb pymysql的模块的基本的使用 # 导入pymysql模块 import pymysq ...

  9. 第八章| 3. MyAQL数据库|Navicat工具与pymysql模块 | 内置功能 | 索引原理

    1.Navicat工具与pymysql模块 在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时,可以使用可视化工具Navicat,以图形界面的形式操作MySQL数 ...

随机推荐

  1. sz 命令

    sz命令 下载文件命令 sz  文件名

  2. mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法

    mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法: 语法: TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2) 说明: 返回日 ...

  3. Miller rabin

    蛤蛤,终于基本上搞懂了 #include<iostream> #include<cstdio> using namespace std; long long num[10]={ ...

  4. 前端HTML基础

    1.0开发工具介绍 sublime的使用技巧链接 HTML特殊符号表 1.1 html概念 超文本标记语言(Hypertext Markup Language),属于一种描述性的标记语言(markup ...

  5. Apache.Tomcat 调用Servlet原理之Class类的反射机制,用orc类解释

    有一个兽人类 package com.swift.servlet; public class OrcDemo { private int hp; private int mp; private int ...

  6. hdu_3501_Calculation 2

    Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...

  7. mysql中列属性

    mysql列属性包括:NULL .default.comment.primary key.unique key 一.NULL定义方式:NULL(默认) NOT NULL 空属性有2个值,mysql数据 ...

  8. CentOS使用yum安装drbd

    CentOS 6.x系统要升级到最新的内核才支持 CentOS 6.x rpm -ivh http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noa ...

  9. php如何连接mysql,并操纵后台服务器运作的过程

    PHP,一个嵌套的缩写名称,是英文超级文本预处理语言(PHP:Hypertext Preprocessor)的缩写.PHP 是一种 HTML 内嵌式的语言,PHP与微软的ASP颇有几分相似,都是一种在 ...

  10. ethereum(以太坊)(十二)--应用(二)__投票(基础总和)

    编写应用合约之前,先弄清它的逻辑,有助于我们更好的部署合约 pragma solidity ^0.4.21; pragma experimental ABIEncoderV2; contract vo ...