美文网首页
python连接mysql数据库(-)

python连接mysql数据库(-)

作者: 洛水青柳2017 | 来源:发表于2017-10-24 09:55 被阅读0次

'''pycharm中第三方库的安装:http://www.cnblogs.com/duwangdan/p/6872750.html'''

自定义函数调用库连接数据库

-------------------------------------------连接数据库的代码------------------------------------------------------------------------

(代码的规范程度需要调整)

#!/usr/bin/env pytho

# -*- coding:utf-8 -*-

defpsql_sjsg (str):

importpymysql

conn = pymysql.connect(host='120.132.67.103',

port=3306,

user='UserSelect',

passwd='1q2w3e4r5t',

db='bingosjsg',

charset='utf8')        #连接数据库

cursor=conn.cursor()         #打开游标

sql= str                               #查询语句

try:

    effect_row= cursor.execute(sql)# 执行查询

    result=cursor.fetchall()# 打印所有查询结果

    conn.commit()# 提交事务

exceptExceptionase :#要是发生错误

    print(e)#打印错误原因

    conn.rollback()#回滚事务

cursor.close()#关闭游标

conn.close()#关闭连接

returnresult#返回查询结果

'''pycharm中库的安装:http://www.cnblogs.com/duwangdan/p/6872750.html'''

print(psql_sjsg("select*from  createrole limit 10"))

相关文章

网友评论

      本文标题:python连接mysql数据库(-)

      本文链接:https://www.haomeiwen.com/subject/bjwauxtx.html