美文网首页
pymysql的用法

pymysql的用法

作者: 王小鱼鱻 | 来源:发表于2017-10-13 02:41 被阅读149次

一、首先要安装MySQL,我安装的mysq5.7的;
具体安装步骤可以自行百度,或者参考这个win10安装MYSQL5.7

二、启动MySQL,
启动:net start MySQL
停止:net stop MySQL
卸载:net delete MySQL

其他数据库操作自行百度;

三、安装pymysql模块:
pip直接安装即可;

四、基本的增删改查的操作:

#coding:utf-8

# import MySQLdb python3不支持MySQLdb
import pymysql

#打开数据库,数据库可自己创建,create database test;
con = pymysql.connect(host='localhost', user='root', passwd='登录密码', db='test', port=3306, charset='utf8')
#通过cursor()创建一个游标对象
cur = con.cursor()

#建表
cur.execute(' CREATE TABLE student (id int not null auto_increment primary key,name varchar(20),age int, sex varchar(2))')

#插入数据
data="'Alice',16,女"
cur.execute(' INSERT INTO person (name,age) VALUES (%s)'%data)

cur.execute(' INSERT INTO person (name,age) VALUES (%s,%s)',('Alex',20,'男'))

cur.executemany(' INSERT INTO person (name,age) VALUES (%s,%s)',[('sara',24,'女'),('开心麻花',30,'男')])

#提交操作
con.commit()

#查询表中的数据
cur.execute('SELECT * FROM person')

#fetchall()获取所有数据,返回一个二维的列表
res = cur.fetchall()
for line in res:
    print(line)

    # fetchone()获取其中的一个结果,返回一个元组
    cur.execute('SELECT * FROM person')
    res = cur.fetchone()
    print(res)

#修改数据
cur.execute('UPDATE person SET name=%s WHERE id=%s', ('小明',12,'男'))

#删除数据
cur.execute('DELETE FROM person WHERE id=%s', (0,))

con.commit()
con.close()

五、用pymysql跑了一下之前写的天气的爬取:

import requests
import json
import pymysql


#打开数据库
con = pymysql.connect(host='localhost', user='root', passwd='wjx411527', db='test', port=3306, charset='utf8')

#通过cursor方法创建一个游标对象
cur = con.cursor()

#建表
cur.execute(' CREATE TABLE tianqi (id int not null auto_increment primary key,date int ,date2 varchar(20),history_rain varchar(20),history_max int,history_min int,tmax varchar(10),tmin varchar(10),time varchar(20),w1 varchar(20),wd1 varchar(20),alins varchar(20),als varchar(20))')


class Weather():

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
                    'Referer': 'http://www.weather.com.cn/weather40d/101280601.shtml'
        }
    # 新建列表,用来存储获取的数据
    all_info = []

    def __init__(self):
        pass

    # 获取一年的天气数据
    def get_data(self):
        global year
        month = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
        for i in month:
            url = 'http://d1.weather.com.cn/calendar_new/' + str(year) + '/101280601_' + str(year) + str(
                i) + '.html?_=1496558858156'
            html = requests.get(url, headers=self.headers).content
            global datas
            datas = json.loads(html[11:])
            self.get_info()

    # 获取天气的具体数据
    def get_info(self):
        for data in datas:
            if data['date'][:4] == year:
                date = data['date']
                date2 = data['nlyf'] + data['nl']
                history_rain = data['hgl']
                history_max = data['hmax']
                history_min = data['hmin']
                tmax = data['max']
                tmin = data['min']
                time = data['time']
                w1 = data['w1']
                wd1 = data['wd1']
                alins = data['alins']
                als = data['als']

            #先把去重的数据存储到列表all_info
            info = (date,date2,history_rain,history_max,history_min,tmax,tmin,time,w1,wd1,alins,als )
            if info not in self.all_info:
                self.all_info.append(info)

    #把数据存到sqlite
    def store_pymysql(self):
        self.get_data()
        for one_info in self.all_info:
            cur.execute(' INSERT INTO tianqi (date,date2,history_rain,history_max,history_min,tmax,tmin,time,w1,wd1,alins,als) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',one_info)
            con.commit()


if __name__ == '__main__':
        year = '2017'
        tianqi = Weather()
        tianqi.store_pymysql()
        # 关闭数据库连接
        con.close()

运行结果:

天气

写法和sqlite的基本类似,可以和上一篇sqlite的比较一下;
几个小点注意一下:
1、mysql的数据库要提前创建好;
2、mysql在创建表时必须要表明每列的数据类型;

mysql这里用的可视化工具是navicat,可以自行百度下载;

相关文章

  • pymysql的用法

    一、首先要安装MySQL,我安装的mysq5.7的;具体安装步骤可以自行百度,或者参考这个win10安装MYSQL...

  • Pymysql基本用法

    -- 取数据 count = cursor.execute("select * from goods;") pri...

  • pymysql的常见用法

    从mysql 中导出数据: 连接池: 相比上一种方式,连接池能提高性能,主要表现在: 1.创建新连接的时候,可以从...

  • pymysql connect 模块

    connections 模块 类:Connection 用法:执行pymysql.connect()得到。而不是构...

  • MySQL(四)

    与Python交互 使用pymysql来代替MySQLdb模块,它们用法非常相似。 Connection对象 用于...

  • 12.Mysql数据库实战

    什么是pymysql、pymysql的安装、pymysql连接对象、pymysql游标对象、案例 Part 1 -...

  • 10.PyMySQL模块

    PyMySQL模块 安装PyMySQL模块pip install pymysql PyMySQL查询数据impor...

  • python3操作mysql示例代码

    PyMySQL 安装在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装。PyMySQL 下载地址:...

  • Python——MySql操作

    pymysql pymysql 的GitHub地址:https://github.com/PyMySQL/PyMy...

  • GitHub上一个用Python写的MySQL库

    https://github.com/PyMySQL/PyMySQL PyMySQL:纯 pyton 写的 mys...

网友评论

      本文标题:pymysql的用法

      本文链接:https://www.haomeiwen.com/subject/couayxtx.html