PyMysql的几个重要方法

  1. connect函数:连接数据库,根据连接的数据库类型不同,该函数的参数也不相同。connect函数返回Connection对象。
  2. cursor方法:获取操作数据库的Cursor对象。cursor方法属于Connection对象。
  3. execute方法:用于执行SQL语句,该方法属于Cursor对象。
  4. commit方法:在修改数据库后,需要调用该方法提交对数据库的修改,commit方法属于Cursor对象。
  5. rollback方法:如果修改数据库失败,一般需要调用该方法进行数据库回滚操作,也就是将数据库恢复成修改之前的样子
  6. from pymysql import *
    import json def connectDB():
    db = connect('127.0.0.1','root','password','databasename')
    return db
    db = connectDB() def creatTable(db):
    cursor = db.cursor()
    sql = '''
    CREATE TABLE Persons
    (
    id INT PRIMARY KEY NOT NULL,
    name TEXT NOT NULL,
    age INT NOT NULL,
    address CHAR(50),
    salary REAL
    );
    '''
    try:
    cursor.execute(sql)
    db.commit()
    return True
    except:
    db.rollback()
    return False def insertRecords(db):
    cursor = db.cursor()
    try:
    cursor.execute('DELETE FROM persons')
    cursor.execute("INSERT INTO persons(id,name,age,address,salary)\
    VALUES(1,'Paul',32,'California',2000.00)");
    cursor.execute("INSERT INTO persons(id,name,age,address,salary)\
    VALUES(2,'Allen',25,'Texas',3000.00)");
    cursor.execute("INSERT INTO persons(id,name,age,address,salary)\
    VALUES(3,'Teddy',23,'Norway',2500.00)");
    cursor.execute("INSERT INTO persons(id,name,age,address,salary)\
    VALUES(4,'Mark',19,'Rich',5000.00)");
    db.commit()
    return True
    except Exception as e:
    print(e)
    db.rollback()
    return False def selectRecords(db):
    cursor = db.cursor()
    sql = 'SELECT name,age,address,salary FROM Persons ORDER BY age DESC'
    cursor.execute(sql)
    results = cursor.fetchall()
    print(results)
    fields = ['name','age','address','salary']
    records = []
    for row in results:
    records.append(dict(zip(fields,row)))
    return json.dumps(records) if creatTable(db):
    print('成功创建Persons表')
    else:
    print('persons表已经存在') if insertRecords(db):
    print('成功插入数据')
    else:
    print('插入记录失败') print(selectRecords(db))
    db.close()

      

pymysql -转自https://www.cnblogs.com/chenhaiming/p/9883349.html#undefined的更多相关文章

  1. 访问路径:https://i.cnblogs.com/posts?categoryid=925678

    https://i.cnblogs.com/posts?categoryid=925678

  2. URL https://i.cnblogs.com/EditPosts.aspx?opt=1

    URL url = new URL("https://i.cnblogs.com");URL url1 = new URL(url, "EditPosts.aspx?op ...

  3. 随笔二-https://www.cnblogs.com/shang1680/p/9657994.html

    作业要求来自https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 GitHub远程仓库的地址:https://github.com/ ...

  4. 211806189杨昊辰 https://www.cnblogs.com/honey1433223/

    211806189杨昊辰 https://www.cnblogs.com/honey1433223/

  5. https://www.cnblogs.com/h2zZhou/p/5440271.html

    https://www.cnblogs.com/h2zZhou/p/5440271.html

  6. https://www.cnblogs.com/soundcode/p/4174410.html

    https://www.cnblogs.com/soundcode/p/4174410.html 1.首先要在服务器端新建一个网站axpx页 然后再网站的后台写代码获取winform传过来的文件名. ...

  7. https://www.cnblogs.com/yudanqu/p/9467803.html

    https://www.cnblogs.com/yudanqu/p/9467803.html

  8. 转发自:一像素 十大经典排序算法(动图演示)原链接:https://www.cnblogs.com/onepixel/articles/7674659.html 个人收藏所用 侵删

    原链接:https://www.cnblogs.com/onepixel/articles/7674659.html     个人收藏所用   侵删 0.算法概述 0.1 算法分类 十种常见排序算法可 ...

  9. @无痕客 https://www.cnblogs.com/wuhenke/archive/2012/12/24/2830530.html 通篇引用

    无痕客 https://www.cnblogs.com/wuhenke/archive/2012/12/24/2830530.html 关于Async与Await的FAQ 关于Async与Await的 ...

随机推荐

  1. virtualenvwrappers pipreqs 踩坑

    virtualenvwrappers 1.安装 pip install virtualenvwrapper 在~/.bashrc 中写入 export WORKON_HOME=~/.virtualen ...

  2. EF框架引用问题

    安装EF框架时,从NuGet上安装 EF 安装完成以后仍然报错误 这个错误  是因为EF实体数据模型未引用System.data.entity  这个DLL ,记一下以防止以后忘记

  3. idea 启动项目提示 Command line is too long. Shorten command line for Application or also for Spring Boot default configuration.

    在.idea 文件夹中打开workspace.xml文件找到<component name="PropertiesComponent">,在标签里加一行  <pr ...

  4. Thing in java 第四章,控制执行流程,练习题答案

    /** * Created by Sandy.Liu on 2018/7/19. * Thinking in java, version 4, chapter 4, practice 1 * Writ ...

  5. nginx添加ssl证书

    ssl的证书是通过docker nginx letsencrypt 这篇随笔生成的,下面介绍如何在nginx中添加ssl 这个为全部配置, 需要替换你自己的域名,配置中强制https了 server ...

  6. PlantUml 使用代码画各种图

    资源 网址 官方github https://github.com/plantuml/plantuml 官方网站 http://plantuml.com/zh/ mac 下,webstorm 中使用 ...

  7. 15.1 打开文件时的提示(不是dos格式)去掉头文件

    1.用ultraedit打开文件时,总提示不是DOS格式 2.把这个取消.dos格式只是用来在unix下读写内容的,此功能禁用即可.

  8. 利用工具将数据库中的表导出到word中

    1.动软代码生成器 效果图: 数据库设计说明书中的一项,刚好我负责写这个文档, 18张表,前两张表是自己画表格自己填充内容,写到第三张表的时候就已经崩溃了(我觉得我耐力还是够的,怎么说也画完了两张表呢 ...

  9. @cacheevict 清除多个key

    @Caching(evict={@CacheEvict(“a1”),@CacheEvict(“a2”,allEntries=true)}): 参考:https://www.cnblogs.com/ha ...

  10. 调整Eclipse中代码字体字号

    Window-->preferences-->general-->appearence-->Colors and fonts中的Basic节点选text font,Edit一下