1.操作mysql的标准流程

import pymysql
conn = pymysql.connect(host = "127.0.0.1", port = 3306,user = "root", passwd = "*******",db = "homework",charset = "utf8")
cursor = conn.cursor()
r = cursor.execute("show tables")
conn.commit()
cursor.close()
conn.close()

上述代码实现了在pycharm下操作mysql的一个标准流程。其中的重点是:

#.创建连接

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1'

#.创建游标

cursor = conn.cursor()

#.执行语句

游标. execute(“”)  其中引号中为mysql语句

#.提交

conn.commit()

#.关闭游标和连接

cursor.close() conn.close()

2.获取自增ID

new_id = cursor.lastrowid

3.查询数据

# 获取第一行数据

row_1 = cursor.fetchone()

# 获取前n行数据

# row_2 = cursor.fetchmany(3)

# 获取所有数据

# row_3 = cursor.fetchall()

这其中有指针的移动 可用cursor.scroll(1,mode='relative')相对移动有正负对应着上下移动 cursor.scroll(1,mode='absolute')移动到绝对的位置。

通过设置cursor括号后的参数控制取数据时的格式,如:字典 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

py操作mysql的更多相关文章

  1. python web.py操作mysql数据库,实现对数据库的增删改查操作

    使用web.py框架,实现对mysql数据库的增删改查操作: 该示例代码中连接的是本地数据库testdb,user表,表结构比较简单,只有两个字段:mobile和passwd,类型均为字符型 实际应用 ...

  2. LightMysql:为方便操作MySQL而封装的Python类

    原文链接:http://www.danfengcao.info/python/2015/12/26/lightweight-python-mysql-class.html mysqldb是Python ...

  3. Python操作MySQL以及中文乱码的问题

    Python操作MySQL需要安装Python-MySQL可以从网上搜索一下,和一般的Python包一样安装 安装好之后,模块名字叫做MySQLdb ,在Window和Linux环境下都可以使用,试验 ...

  4. 使用python操作mysql数据库

    这是我之前使用mysql时用到的一些库及开发的工具,这里记录下,也方便我查阅. python版本: 2.7.13 mysql版本: 5.5.36 几个python库 1.mysql-connector ...

  5. flask 操作mysql的两种方式-sqlalchemy操作

    flask 操作mysql的两种方式-sqlalchemy操作 二.ORM sqlalchemy操作 #coding=utf-8 # model.py from app import db class ...

  6. flask 操作mysql的两种方式-sql操作

    flask 操作mysql的两种方式-sql操作 一.用常规的sql语句操作 # coding=utf-8 # model.py import MySQLdb def get_conn(): conn ...

  7. python操作mysql——mysql.connector

    连接mysql, 需要mysql connector, conntector是一种驱动程序,python连接mysql的驱动程序,mysql官方给出的名称为connector/python, 可参考m ...

  8. python大法好——操作mysql

    python操作mysql数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库 ...

  9. python操作三大主流数据库(6)python操作mysql⑥新闻管理后台功能的完善(增、ajax异步删除新闻、改、查)

    python操作mysql⑥新闻管理后台功能的完善(增.删.改.查)安装表单验证D:\python\python_mysql_redis_mongodb\version02>pip instal ...

随机推荐

  1. 获取DLL中的方法名称

      OpenFileDialog obj = new OpenFileDialog(); if (obj.ShowDialog() == System.Windows.Forms.DialogResu ...

  2. CodeForces#275--DIV 2--B(BinarySearch)(!!)

    B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input stand ...

  3. Spring In Action ②

    初始化和销毁Bean init-method && destory-method <bean id="auditorium" class="test ...

  4. 厉害了,摩托罗拉发布全球首款支持VR和AR的手机MotoZ

    目前支持谷歌daydream移动VR生态系统的手机型号并不多,除了谷歌自家的Pixel系列外,还有华为推出的mate9系列.如今又到了一位新成员,即升级到Android 7.0后的Moto Z. 更为 ...

  5. 一些有用的HTML5 pattern属性

    最近在做手机页面时,遇到数字输入的键盘的问题,之前的做法只是一刀切的使用 type="tel",不过一直觉得九宫格的电话号码键盘上的英文字母太碍事了.于是想要尝试其它的实现方案,最 ...

  6. Android GPS 取经纬度

    // 获取位置管理服务 private LocationManager locationManager;3 String mProviderName = ""; private v ...

  7. CentOS 命令【备忘】

    1.查看物理cpu个数 grep 'physical id' /proc/cpuinfo | sort -u | wc -l 2.查看核心数量 grep 'core id' /proc/cpuinfo ...

  8. 【poj2828】Buy Tickets

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  9. Android --时间控件的使用

    1. mian.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...

  10. NOI模拟赛Day5

    T1 有and,xor,or三种操作,每个人手中一个数,求和左边进行某一种运算的最大值,当t==2时,还需要求最大值的个数. test1 20% n<=1000 O(n^2)暴力 test2 2 ...