不废话直接上代码:

import pymysql

class MysqlConnection:
'''
单例模式获取数据库链接实例
'''
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = pymysql.connect( *args, **kwargs)
return cls._instance if __name__ == '__main__':
conn = MysqlConnection(db='spider_data',user='root',password='root',host='127.0.0.1')
# 获取游标
cursor = conn.cursor()
sql = 'select {} from {} limit {}'.format(
','.join(('img_url','link','title')),
'chinaz',
'0,4'
)
# 执行sql语句,返回受影响的行数
print(cursor.execute(sql))
# 读取一条数据
print(cursor.fetchone())
# 读取多条,结果是一个tupe类型
print(cursor.fetchmany(2))
# 游标置零
cursor.rownumber = 0
# 读取全部
print(cursor.fetchall())
# 插入数据
sql = 'insert into {}({}) values({})'.format('chinaz', ','.join(('img_url', 'link', 'title')),'\"img1\",\"link1\",\"title1\"')
try:
cursor.execute(sql)
# 提交保存到数据库中
conn.commit()
except:
# 回滚
conn.rollback() # 关闭游标
cursor.close()
# 关闭和数据库的连接
conn.close() # 执行后的打印结果
"""
4
('http://topimg.chinaz.com/WebSiteimages/pkueducn/56abaa19-b68f-4338-b710-561168d3686e_2016_s.png', 'http://top.chinaz.com/Html/site_pku.edu.cn.html', '北京大学')
(('http://topimg.chinaz.com/WebSiteimages/fudaneducn/d95f4aa6-8fdc-4b3d-b2d7-94f4be58de77_2016_s.png', 'http://top.chinaz.com/Html/site_fudan.edu.cn.html', '复旦大学'), ('http://topimg.chinaz.com/WebSiteimages/wwwscueducn/79382c04-0541-4779-b05a-936a518a6cc8_2015_s.png', 'http://top.chinaz.com/Html/site_scu.edu.cn.html', '四川大学'))
(('http://topimg.chinaz.com/WebSiteimages/pkueducn/56abaa19-b68f-4338-b710-561168d3686e_2016_s.png', 'http://top.chinaz.com/Html/site_pku.edu.cn.html', '北京大学'), ('http://topimg.chinaz.com/WebSiteimages/fudaneducn/d95f4aa6-8fdc-4b3d-b2d7-94f4be58de77_2016_s.png', 'http://top.chinaz.com/Html/site_fudan.edu.cn.html', '复旦大学'), ('http://topimg.chinaz.com/WebSiteimages/wwwscueducn/79382c04-0541-4779-b05a-936a518a6cc8_2015_s.png', 'http://top.chinaz.com/Html/site_scu.edu.cn.html', '四川大学'), ('http://topimg.chinaz.com/WebSiteimages/wwwtsinghuaeducn/49499b0d-fc15-4be3-8ffe-a52d26469321_2017_s.png', 'http://top.chinaz.com/site_www.tsinghua.edu.cn.html', '清华大学')) """

pymsql简单的使用的更多相关文章

  1. pymsql的简单实用方法

    在进行本文以下内容之前需要注意: 1.你有一个MySQL数据库,并且已经启动. 2.你有可以连接该数据库的用户名和密码 3.你有一个有权限操作的database 连接数据库 #导入pymsql imp ...

  2. day12---python mysql pymsql sqlalchemy ORM

    RDBMS 术语 在我们开始学习MySQL 数据库前,让我们先了解下RDBMS的一些术语: 数据库: 数据库是一些关联表的集合.. 数据表: 表是数据的矩阵.在一个数据库中的表看起来像一个简单的电子表 ...

  3. 【python】-- pymsql 操作MySQL

    pymysql 对MySQL数据库进行简单数据操作python模块主要是:MySQLdb.pymsql,MySQLdb模块主要用于python2.X,而python3.X则使用pymsql,pymys ...

  4. 进击的Python【第十二章】:mysql介绍与简单操作,sqlachemy介绍与简单应用

    进击的Python[第十二章]:mysql介绍与简单操作,sqlachemy介绍与简单应用 一.数据库介绍 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数 ...

  5. pymsql及事务

    MySQL知识点补充 1.去重 distinct select distinct name,age from t1; # 针对查找出来的结果整行(记录)进行去重,也就是相同行只保存一个 注意点:dis ...

  6. Python pymsql模块

    pymsql pymysql这款第三方库可以帮助我们利用python语言与mysql进行链接 基本使用 首先要下载pymysql pip install pymsql 以下是pymysql的基本使用 ...

  7. 【造轮子】打造一个简单的万能Excel读写工具

    大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...

  8. Fabio 安装和简单使用

    Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...

  9. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

随机推荐

  1. JAVAEE期末项目------文章发布系统

    项目文档和代码的GitHub地址:https://github.com/xiangbaobaojojo/- 1.项目介绍 (计科四班  蔡春燕 20150141401)和我 陈香宇(计科四班  201 ...

  2. Windows的Shell命令又是Windows的CMD命令。也就是原来MS-DOS系统保留下来。基本的CMD命令

    Windows常用工具类 记事本 notepad 计算器 calc 画图 mspaint 写字板 write Windows放大镜 magnify 辅助工具管理器 utilman Telnet客户端 ...

  3. npm ERR! Unexpected end of JSON input while parsing near...错误

    问题解决方案在GitHub中: https://github.com/vuejs-templates/webpack/issues/990 总结一下:1.删除package-lock.json 2.进 ...

  4. sublime text常用快捷键及多行光标批量操作教程

    sublime text常用快捷键及多行光标批量操作教程   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a772304419/article/d ...

  5. myEclipse出现cannot paste the clipboard contents into the selected elements报错

    导入jar包报错,cannot paste the clipboard contents into the selected elements,查阅资料让重新打开工程,但依然报错. 最后在本地路径复制 ...

  6. linux设置自启动redis

    vi /etc/init.d/redis # chkconfig: 2345 10 90 # description: Start and Stop redis PATH=/usr/local/bin ...

  7. IntelliJ IDEA 实用快捷键

    psvm--------------主(main)-----------------public staitc void main(String[] args) sout--------------- ...

  8. CMDB-(paramiko模块 -- 实现ssh连接)

    import paramiko # 实现ssh功能的模块 ssh = paramiko.SSHClient() # 实例化对象 ssh.set_missing_host_key_policy(para ...

  9. 爬虫模块介绍--request(发送请求模块)

    爬虫:可见即可爬   # 每个网站都有爬虫协议 基础爬虫需要使用到的三个模块 requests 模块  # 模拟发请求的模块 PS:python原来有两个模块urllib和urllib的升级urlli ...

  10. Unity安卓打包遇到的问题。

    1, android asset packinging tool failed. "路径" package -v -f -F assets.ap_ - Assets found X ...