首页 > 数据库技术 > 详细

python链接数据库pymysql

时间:2018-09-13 19:39:32      阅读:160      评论:0      收藏:0      [点我收藏+]

#python链接数据库pymysql

import pymysql

config = {host : 172.0.0.1,
          user : lijing,
          post : 123456}
#链接数据库
db = pymysql.connect(**config)
#获取游标
cursor = db.cursor()

try:
    #执行sql命令
    sql = "create table if not exists 表名"
    cursor.execute(sql)
    
    #获取返回的结果一条
    cursor.fetchone()
    #获取返回结果3条
    cursor.fetchmany(3)
    #获取返回结果全部
    cursor.fetchall()

    #提交到数据库执行
    db.commit()
except Exception as e:
    raise e
    #回滚
    db.rollback()
finally:
    #关闭游标
    cursor.close()
    #关闭数据库链接
    db.close()

 

python链接数据库pymysql

原文:https://www.cnblogs.com/lijinglj/p/9642197.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!