存入数据库
2021/6/21 2:27:53
本文主要是介绍存入数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
读取json文件的数据
with open('./jingqu/datapoi.json', 'r') as file: data = file.read() data = json.loads(data)
链接数据库
def du_sql(): mydb = mysql.connector.connect( host="localhost", user="root", password="password", database="scence", auth_plugin="mysql_native_password" ) dbpath = mydb.cursor() savaDataSql(dbpath) mydb.commit()
定于函数分析数据存入数据库
def savaDataSql(dbpath): cur = dbpath try: for each in data: name = each['name'] type = each['type'] if(each['address']!=None): if(len(each['address'])==0): address = '' else: address = each['address'] else: address = '' adname = each['adname'] location = each['location'] citycode = each['citycode'] adcode = each['adcode'] pname = each['pname'] cityname = each['cityname'] date = time.strftime("%Y-%m-%d",time.localtime()) sql = "INSERT INTO scence1 (name,type,address,adname,location,adcode,citycode,pname,cityname,data) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" var = (name,type,address,adname,location,adcode,citycode,pname,cityname,date) cur.execute(sql,var) except : print(name)
这篇关于存入数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-15JavaMailSender是什么,怎么使用?-icode9专业技术文章分享
- 2024-11-15JWT 用户校验学习:从入门到实践
- 2024-11-15Nest学习:新手入门全面指南
- 2024-11-15RestfulAPI学习:新手入门指南
- 2024-11-15Server Component学习:入门教程与实践指南
- 2024-11-15动态路由入门:新手必读指南
- 2024-11-15JWT 用户校验入门:轻松掌握JWT认证基础
- 2024-11-15Nest后端开发入门指南
- 2024-11-15Nest后端开发入门教程
- 2024-11-15RestfulAPI入门:新手快速上手指南