博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pymysql
阅读量:4670 次
发布时间:2019-06-09

本文共 3718 字,大约阅读时间需要 12 分钟。

一,IDE工具介绍

生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具
下载链接:https://pan.baidu.com/s/1bpo5mqj

掌握:#1. 测试+链接数据库#2. 新建库#3. 新建表,新增字段+类型+约束#4. 设计表:外键#5. 新建查询#6. 备份库/表#注意:批量加注释:ctrl+?键批量去注释:ctrl+shift+?键

二,pymysql模块

先安装 pip3 install pymysql  才能导入这个模块import pymysqlconn=pymysql.connect(    host='127.0.0.1',    port=3306,    user='root',    password='123',    database='db42',    charset='utf8')cursor=conn.cursor(pymysql.cursors.DictCursor)# rows=cursor.execute('show tables;')rows=cursor.execute('select * from class;')print(rows)## print(cursor.fetchone())# print(cursor.fetchmany(2))# print(cursor.fetchone())# print(cursor.fetchall())## print(cursor.fetchall())# cursor.scroll(3,'absolute')# print(cursor.fetchone())## print(cursor.fetchone())# print(cursor.fetchone())# cursor.scroll(1,'relative')# print(cursor.fetchone())## cursor.close()# conn.close()import pymysql #pip3 install pymysqlconn=pymysql.connect(    host='127.0.0.1',    port=3306,    user='root',    password='123',    database='db42',    charset='utf8')cursor=conn.cursor(pymysql.cursors.DictCursor)inp_user=input('用户名>>:').strip() #inp_user=""inp_pwd=input('密码>>:').strip() #inp_pwd=""sql="select * from user where name='%s' and password='%s'" %(inp_user,inp_pwd)print(sql)rows=cursor.execute(sql)if rows:    print('登录成功')else:    print('登录失败')cursor.close()conn.close()直接改变了sql  绕过了密码和用户名#在服务器端防止sql注入问题:不要自己拼接字符串,让pymysql模块去拼接import pymysql #pip3 install pymysqlconn=pymysql.connect(    host='127.0.0.1',    port=3306,    user='root',    password='123',    database='db42',    charset='utf8')cursor=conn.cursor(pymysql.cursors.DictCursor)inp_user=input('用户名>>:').strip() #inp_user=""inp_pwd=input('密码>>:').strip() #inp_pwd=""sql="select * from user where name=%s and password=%s"print(sql)rows=cursor.execute(sql,(inp_user,inp_pwd))if rows:    print('登录成功')else:    print('登录失败')cursor.close()conn.close()

三,、增、删、改:conn.commit()

import pymysql #pip3 install pymysqlconn=pymysql.connect(    host='127.0.0.1',    port=3306,    user='root',    password='123',    database='db42',    charset='utf8')cursor=conn.cursor(pymysql.cursors.DictCursor)# sql='insert into user(username,password) values(%s,%s)'# rows=cursor.execute(sql,('EGON','123456'))# print(rows)# print(cursor.lastrowid)# rows=cursor.execute('update user set username="alexSB" where id=2')# print(rows)# 一次插入多行记录sql='insert into user(username,password) values(%s,%s)'rows=cursor.executemany(sql,[('lwz','123'),('evia','455'),('lsd','333')])print(rows)print(cursor.lastrowid)conn.commit() # 只有commit提交才会完成真正的修改cursor.close()conn.close()

四,查:fetchone,fetchmany,fetchall

fetchone(查询单条) fetchmany(查询多条) fetchall(查询所有)import pymysqlconn=pymysql.connect(    host='127.0.0.1',    port=3306,    user='root',    password='123',    database='db42',    charset='utf8')cursor=conn.cursor(pymysql.cursors.DictCursor)# rows=cursor.execute('show tables;')rows=cursor.execute('select * from class;')print(rows)## print(cursor.fetchone())# print(cursor.fetchmany(2))# print(cursor.fetchone())# print(cursor.fetchall())## print(cursor.fetchall())# cursor.scroll(3,'absolute')# print(cursor.fetchone())## print(cursor.fetchone())# print(cursor.fetchone())# cursor.scroll(1,'relative')# print(cursor.fetchone())## cursor.close()# conn.close()

五,获取最后

------查看表中最后一行的iDimport pymysqlconn=pymysql.connect(host='localhost',user='root',password='123456',             database='day47',charset='utf8')cursor=conn.cursor()sql='insert into user1(user,password) values(%s,%s);'rows=cursor.execute(sql,('alex','123'))# rows=cursor.executemany(sql,[('yuanhao','123'),('laowu','123'),('kgf','12323')])conn.commit()print(cursor.lastrowid)  #查看表中最后一行的iDcursor.close()conn.close()

 

转载于:https://www.cnblogs.com/maojiang/p/9030015.html

你可能感兴趣的文章
自我反省
查看>>
反射,得到Type引用的三种方式
查看>>
pl sql练习(2)
查看>>
Problem B: 判断回文字符串
查看>>
谷歌浏览器,添加默认搜索引擎的搜索地址
查看>>
数据结构化与保存
查看>>
C# .net 获取程序运行的路径的几种方法
查看>>
为什么需要Docker?
查看>>
国内5家云服务厂商 HTTPS 安全性测试横向对比
查看>>
how to control project
查看>>
转 python新手容易犯的6个错误
查看>>
第四节 -- 列表
查看>>
Python入门学习笔记4:他人的博客及他人的学习思路
查看>>
webstorm里直接调用命令行
查看>>
关联规则算法之FP growth算法
查看>>
对数组序列进行洗牌
查看>>
决策树
查看>>
团队作业
查看>>
如何避免在简单业务逻辑上面的细节上面出错
查看>>
win7,Ubuntu 12.04 双系统修改启动项顺序三方法
查看>>