python连接数据库

连接的是Mysql数据库

首先,在idea中执行命令 pip install pymysql  安装可执行的文件

文件安装完成后,一般会在项目的venv---->Lib---->site packages 下,需要将pymysql文件夹放到Lib下面



此时.py中的import pymysql不报错

我们就可以使用操作数据库的代码

import pymysql
db =pymysql.connect( port=3306,user='root',password='root',db='asso',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
cusor = db.cursor()
cusor.execute("select * from org")
for row in cusor.fetchall():
    print(row)
db.close();

参数如上


打印结果:

D:\pythonOut\venv\Scripts\python.exe D:/pythonOut/casasa1.py
{'id': 1, 'orgType': '1', 'managerId': '0', 'companyCode': '10000000', 'name': '总公司', 'alias': '北京', 'postAddress': None, 'postCode': None, 'phoneNumber': None, 'mobile': None, 'email': None, 'faxNumber': None, 'personId': None, 'comCode': '10000000', 'businessNature': None, 'orgCode': None, 'customerCode': None, 'agentCode': None, 'proId': None, 'isBalance': None, 'balanceFlag': None, 'payFlag': None, 'coinsFlag': None, 'coinsRate_gb': None, 'coinsRate_lb': None, 'customerType': None, 'sellerId': None, 'handlerId': None, 'handler1Id': None, 'approverId': None, 'operatorId': None, 'generateMethod': None, 'status': '1', 'isParent': 'true', 'iconSkin': 'pIcon01', 'createId': None, 'delId': None, 'creatTime': None, 'updateTime': None, 'conment': None, 'spare01': None, 'spare02': None, 'spare03': None, 'spare04': None}
{'id': 2, 'orgType': '2', 'managerId': '0', 'companyCode': '10000000', 'name': '总公司外部客户', 'alias': '北京', 'postAddress': None, 'postCode': None, 'phoneNumber': None, 'mobile': None, 'email': None, 'faxNumber': None, 'personId': None, 'comCode': '10000000', 'businessNature': None, 'orgCode': None, 'customerCode': None, 'agentCode': None, 'proId': None, 'isBalance': None, 'balanceFlag': None, 'payFlag': None, 'coinsFlag': None, 'coinsRate_gb': None, 'coinsRate_lb': None, 'customerType': None, 'sellerId': None, 'handlerId': None, 'handler1Id': None, 'approverId': None, 'operatorId': None, 'generateMethod': None, 'status': '1', 'isParent': 'true', 'iconSkin': 'pIcon01', 'createId': None, 'delId': None, 'creatTime': None, 'updateTime': None, 'conment': None, 'spare01': None, 'spare02': None, 'spare03': None, 'spare04': None}
{'id': 40, 'orgType': '1', 'managerId': '1', 'companyCode': '44000000', 'name': '广东省分公司', 'alias': '广东省分公司', 'postAddress': '', 'postCode': None, 'phoneNumber': '135', 'mobile': '', 'email': '', 'faxNumber': '', 'personId': '', 'comCode': '44000000', 'businessNature': '1', 'orgCode': None, 'customerCode': None, 'agentCode': None, 'proId': '', 'isBalance': None, 'balanceFlag': None, 'payFlag': None, 'coinsFlag': None, 'coinsRate_gb': None, 'coinsRate_lb': None, 'customerType': None, 'sellerId': None, 'handlerId': None, 'handler1Id': None, 'approverId': None, 'operatorId': None, 'generateMethod': None, 'status': '1', 'isParent': 'true', 'iconSkin': 'pIcon01', 'createId': '1', 'delId': '', 'creatTime': datetime.datetime(2017, 8, 4, 0, 0), 'updateTime': datetime.datetime(2017, 8, 21, 0, 0), 'conment': None, 'spare01': None, 'spare02': None, 'spare03': None, 'spare04': None}
{'id': 41, 'orgType': '2', 'managerId': '2', 'companyCode': '44000000', 'name': '广东省分公司外部客户', 'alias': '广东省分公司', 'postAddress': '', 'postCode': None, 'phoneNumber': '', 'mobile': '', 'email': '', 'faxNumber': '', 'personId': '', 'comCode': '44000000', 'businessNature': '1', 'orgCode': None, 'customerCode': None, 'agentCode': None, 'proId': '', 'isBalance': None, 'balanceFlag': None, 'payFlag': None, 'coinsFlag': None, 'coinsRate_gb': None, 'coinsRate_lb': None, 'customerType': None, 'sellerId': None, 'handlerId': None, 'handler1Id': None, 'approverId': None, 'operatorId': None, 'generateMethod': None, 'status': '1', 'isParent': 'true', 'iconSkin': 'pIcon01', 'createId': '1', 'delId': '', 'creatTime': datetime.datetime(2017, 8, 4, 0, 0), 'updateTime': datetime.datetime(2017, 8, 4, 0, 0), 'conment': None, 'spare01': None, 'spare02': None, 'spare03': None, 'spare04': None}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值