首先安装pymysql  pip install pymysql

准备数据库:创建一个数据库testdb

mysql实例:

import pymysql

#打开数据库连接,使用数据库所在的IP127.0.0.1,数据库的用户名和密码,要操作的数据库testdb
db=pymysql.connect('127.0.0.1','root','','testdb') #使用cursor()创建一个游标对象
cursor=db.cursor() #使用 execute() 方法执行 SQL,如果表存在则删除
cursor.execute('drop table if exists user;') #创建表的SQL语句
sql='''create table user (
id int(10) primary key,
user varchar(20)
);
''' #执行sql语句
cursor.execute(sql) #插入数据的SQL语句
sql1="insert into user values(1,'tangqiu');" try:
  #执行SQL语句
    cursor.execute(sql1)
    db.commit()
except :
  #发生错误时回滚
    db.rollback() #查询的SQL语句
sql2='select * from user;'
#执行SQL语句
cursor.execute(sql2) # 获取所有记录列表
result=cursor.fetchall() print(result)
db.close()

最新文章

  1. tomcat 7 WARNING: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to []
  2. Jquery 获取 radio/select选中值
  3. [主页]大牛系列01:Microsoft Research的Johannes Kopf
  4. 开源数据库连接池proxool
  5. 【Android学习】调用系统短信、电话
  6. 源代码分析:LayoutParams的wrap_content, match_parent, 而详细的价值观
  7. iOS开发——打开手机相册,获取图片
  8. vue工程利用pubsub-js实现兄弟组件之间的通信
  9. xml方式封装数据方法
  10. java json转换(一)
  11. vscode隐藏运行ts生成的js文件和map文件
  12. Android实现夜间模式
  13. java通过传入的日期,获取所在周的周一至周日
  14. Android-Kotlin-具名参数
  15. 站在巨人肩上的.NET Core 2.1
  16. AngularJS入门基础——表单验证
  17. 目标检测之2015iccv---objdetection 专题论文
  18. 20165211 获奖感想及java课程总结
  19. RN 中 Native 模块的注入过程
  20. TensorFlow架构与设计:概述

热门文章

  1. OpenFOAM——梯形腔双边驱流
  2. jmap -heap 查看堆内存
  3. Linux系统学习(二)一Linux基本操作
  4. Intellij IDEA 智能补全的 10 个姿势,太牛逼了。。
  5. Centos7安装Redis5.0.5并加入Systemd服务
  6. nodejs爬虫如何设置动态ip以及userAgent
  7. python3新特性函数注释Function Annotations用法分析
  8. Java JDBC利用反射技术将查询结果封装为对象
  9. Python3基础 tuple list转为tuple
  10. LIST<>泛型集合取得对象的属性值