码迷,mamicode.com
首页 > 数据库 > 详细

python链接数据库pymysql

时间:2018-09-13 20:06:45      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:mys   结果   div   cep   try   close   pytho   mysql   com   

#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

标签:mys   结果   div   cep   try   close   pytho   mysql   com   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!