pymysql语法_pymysql的用法

一、首先要安装MySQL,我安装的mysq5.7的;

具体安装步骤可以自行百度,或者参考这个win10安装MYSQL5.7​www.jianshu.com

二、启动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=%sWHERE 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,可以自行百度下载;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用pymysql执行SQL语句时,如果字符串中包含引号,需要进行转义处理,否则会导致SQL语句语法错误或SQL注入攻击。下面介绍两种常见的引号转义处理方法: 1. 使用pymysql.escape_string()函数 pymysql.escape_string()函数可以将字符串中的特殊字符进行转义,包括引号、反斜杠等。示例代码如下: ```python import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', user='root', password='123456', db='test') # 创建游标 cursor = conn.cursor() # 定义要插入的数据 name = 'Tom' age = 20 remark = 'He said, "I\'m fine."' # 对remark字段进行转义处理 remark = pymysql.escape_string(remark) # 执行插入语句 sql = "INSERT INTO users (name, age, remark) VALUES ('%s', '%d', '%s')" % (name, age, remark) cursor.execute(sql) # 提交事务 conn.commit() # 关闭游标和连接 cursor.close() conn.close() ``` 在上面的代码中,使用`pymysql.escape_string()`函数将`remark`字段中的双引号进行了转义处理,转义后的结果为`He said, \"I\'m fine.\"`,这样就可以避免插入语句中的引号导致语法错误。 2. 使用占位符 使用占位符的方法可以避免手动进行引号转义处理,使代码更加简洁和安全。示例代码如下: ```python import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', user='root', password='123456', db='test') # 创建游标 cursor = conn.cursor() # 定义要插入的数据 name = 'Tom' age = 20 remark = 'He said, "I\'m fine."' # 执行插入语句 sql = "INSERT INTO users (name, age, remark) VALUES (%s, %s, %s)" params = (name, age, remark) cursor.execute(sql, params) # 提交事务 conn.commit() # 关闭游标和连接 cursor.close() conn.close() ``` 在上面的代码中,使用了占位符`%s`来代替插入语句中的字符串,然后使用`cursor.execute()`函数的第二个参数`params`来传递参数,由pymysql自动进行转义处理。这样就可以避免手动进行引号转义处理,提高了代码的可读性和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值