程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to insert Python into the database? Please give the complete code

編輯:Python

The main problem is to sort out the data format and insert it into the database . It's not very difficult , Mainly because I just came into contact python, Want to finish the task quickly , I don't want to try . The address of the database has been given , You can log in to check the database format for testing . Thank you for your help .

original json Format :
{
"msg": "ok",
"status": 0,
"data": {
"orderdetail": {
"bak": "",
"crm_mco_billdocument_id": "CO2207010002",
"crm_mco_linkman": "",
"crm_mco_phone": "1399999999",
"crm_mco_address": " Tongliao village, Tongliao City ",
"crm_mco_seller": " Name of salesman ",
"crm_mco_operatedate": "2022-07-01 09:51:22",
"crm_mco_inoutspecies": "15",
"LinkMan": " Contact name ",
"hr_fty_name": " Delivery factory ",
"isCoupon": "0",
"ROW_NUMBER": "1",
"total_num": 210,
"total_tons": 10.500000000000002,
"total_money": 36050
},
"ordergoods": {
"CF00032-0004": {
"data": {
"productid": "CF00032-0004",
"appellation": " The product name ",
"crm_lco_amount": "40.0000",
"crm_lco_famount": "2.0000",
"crm_lco_summoney": "8700.0000",
"crm_lco_price": "217.50000000",
"crm_lco_fprice": "217.50000000",
"spec": "50kg/ package ",
"productunitname": " package ",
"productType": "1",
"nature": " Tons of ",
"crm_lco_isgift": "",
"pstr8": " Product code ",
"ROW_NUMBER": "1"
},
"zeng_crm_lco_amount": "2.0000",
"zeng_crm_lco_famount": ".1000",
"zeng_goods": " Product code ",
"productunitname": " package ",
"nature": " Tons of "
},
"CF00035-0004": {
"data": {
"productid": "CF00035-0004",
"appellation": " The product name ",
"crm_lco_amount": "120.0000",
"crm_lco_famount": "6.0000",
"crm_lco_summoney": "19800.0000",
"crm_lco_price": "165.00000000",
"crm_lco_fprice": "165.00000000",
"spec": "50kg/ package ",
"productunitname": " package ",
"productType": "1",
"nature": " Tons of ",
"crm_lco_isgift": "",
"pstr8": " Product code 2",
"ROW_NUMBER": "3"
},
"zeng_crm_lco_amount": "6.0000",
"zeng_crm_lco_famount": ".3000",
"zeng_goods": " Product code 2",
"productunitname": " package ",
"nature": " Tons of "
},
"CF00039-0004": {
"data": {
"productid": "CF00039-0004",
"appellation": " The product name 3",
"crm_lco_amount": "20.0000",
"crm_lco_famount": "1.0000",
"crm_lco_summoney": "3800.0000",
"crm_lco_price": "190.00000000",
"crm_lco_fprice": "190.00000000",
"spec": "50kg/ package ",
"productunitname": " package ",
"productType": "1",
"nature": " Tons of ",
"crm_lco_isgift": "",
"pstr8": " Product code 3",
"ROW_NUMBER": "5"
},
"zeng_crm_lco_amount": "1.0000",
"zeng_crm_lco_famount": ".0500",
"zeng_goods": " Product code ",
"productunitname": " package ",
"nature": " Tons of "
},
"CF00221-0001": {
"data": {
"productid": "CF00221-0001",
"appellation": " Product code ",
"crm_lco_amount": "20.0000",
"crm_lco_famount": "1.0000",
"crm_lco_summoney": "3750.0000",
"crm_lco_price": "187.50000000",
"crm_lco_fprice": "187.50000000",
"spec": "50kg/ package ",
"productunitname": " package ",
"productType": "1",
"nature": " Tons of ",
"crm_lco_isgift": "",
"pstr8": " Product code ",
"ROW_NUMBER": "7"
},
"zeng_crm_lco_amount": "1.0000",
"zeng_crm_lco_famount": ".0500",
"zeng_goods": " Product code ",
"productunitname": " package ",
"nature": " Tons of "
}
},
"zenglist": [{
"productid": "DG00001-0001",
"appellation": " Product code product code ",
"crm_lco_amount": "1.0000",
"crm_lco_famount": ".0000",
"crm_lco_summoney": ".0000",
"crm_lco_price": ".00000000",
"crm_lco_fprice": ".00000000",
"spec": "-",
"productunitname": " Pieces of ",
"productType": "6",
"nature": "0",
"crm_lco_isgift": " yes ",
"pstr8": " Product code product code ",
"ROW_NUMBER": "9"
}]
}
}

My solution ideas and tried methods
import jsonimport mysql.connectordef insert_data(order_detail): config = {
'host': '111.231.146.150','user': 'mushi','password': 'mushi','port': 16988,'database': 'mushi','charset': 'utf8'} try: con=mysql.connector.connect(**config) cursor = con.cursor() except mysql.connector.error as e: print(' Database connection error !{}'.format(e)) try:# Database insert sql_query_orderdetail=("INSERT INTO `mushi`.`orderdetail` (`crm_mco_billdocument_id`, \ `crm_mco_linkman`, `crm_mco_phone`, `crm_mco_address`, `crm_mco_seller`, `crm_mco_operatedate`, `crm_mco_inoutspecies`, `LinkMan`,\ `hr_fty_name`, `isCoupon`, `ROW_NUMBER`, `total_num`, `total_tons`, `total_money`)" " VALUES (%s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s, %s, %s, %s)" ) order_detailmk={
'crm_mco_billdocument_id':order_detail['data']['orderdetail']['crm_mco_billdocument_id'], 'crm_mco_linkman':order_detail['data']['orderdetail']['crm_mco_linkman'], 'crm_mco_phone':order_detail['data']['orderdetail']['crm_mco_phone'], 'crm_mco_address':order_detail['data']['orderdetail']['crm_mco_address'], 'crm_mco_seller':order_detail['data']['orderdetail']['crm_mco_seller'], 'crm_mco_operatedate':order_detail['data']['orderdetail']['crm_mco_operatedate'], 'crm_mco_inoutspecies':order_detail['data']['orderdetail']['crm_mco_inoutspecies'], 'LinkMan':order_detail['data']['orderdetail']['LinkMan'], 'hr_fty_name':order_detail['data']['orderdetail']['hr_fty_name'], 'isCoupon':order_detail['data']['orderdetail']['isCoupon'], 'ROW_NUMBER':order_detail['data']['orderdetail']['ROW_NUMBER'], 'total_num':order_detail['data']['orderdetail']['total_num'], 'total_tons':order_detail['data']['orderdetail']['total_tons'], 'total_money':order_detail['data']['orderdetail']['total_money'], } sql_query_ordergoods="INSERT INTO `mushi`.`ordergoods` (`crm_mco_billdocument_id`, `productid`, `appellation`, `crm_lco_amount`, `crm_lco_famount`, `crm_lco_summoney`, `crm_lco_price`, `crm_lco_fprice`, `spec`, `productunitname`, `productType`, `nature`, `crm_lco_isgift`, `pstr8`, `ROW_NUMBER`, `zeng_crm_lco_amount`, `zeng_crm_lco_famount`, `zeng_goods`) VALUES (' from ', '', '', NULL, '', NULL, '', '321', NULL, '', NULL, '', '', NULL, '', '', '', '');" order_ordergoodsmk={
'crm_mco_billdocument_id':order_detail['data']['orderdetail']['crm_mco_billdocument_id'], 'productid':order_detail['ordergoods']['CF00000000XX']['data']['productid'],# Find a problem , I can't do it . 'appellation':order_detail['data']['orderdetail']['crm_mco_phone'], 'crm_lco_amount':order_detail['data']['orderdetail']['crm_mco_address'], 'crm_lco_famount':order_detail['data']['orderdetail']['crm_mco_seller'], 'crm_lco_summoney':order_detail['data']['orderdetail']['crm_mco_operatedate'], 'crm_lco_price':order_detail['data']['orderdetail']['crm_mco_inoutspecies'], 'crm_lco_fprice':order_detail['data']['orderdetail']['LinkMan'], 'spec':order_detail['data']['orderdetail']['hr_fty_name'], 'productunitname':order_detail['data']['orderdetail']['isCoupon'], 'productType':order_detail['data']['orderdetail']['ROW_NUMBER'], 'nature':order_detail['data']['orderdetail']['total_num'], 'crm_lco_isgift':order_detail['data']['orderdetail']['total_tons'], 'pstr8':order_detail['data']['orderdetail']['total_money'], 'ROW_NUMBER':order_detail['data']['orderdetail']['total_money'], 'zeng_crm_lco_amount':order_detail['data']['orderdetail']['total_money'], 'zeng_crm_lco_famount':order_detail['data']['orderdetail']['total_money'], 'zeng_goods':order_detail['data']['orderdetail']['total_money'], } cursor.execute(sql_query_orderdetail,order_detailmk) cursor.execute(sql_query_ordergoods,order_ordergoodsmk) except mysql.connector.Error as e: print(' Statement execution error !{}'.format(e)) finally: cursor.close() con.close() text = """{'msg': ' Invalid request ', 'status': 404, 'data': []}""" text = text.replace('\'','\"') a= json.loads(text) print(a['status'])# Program starts jsontext="""{"msg":"ok","status":0,"data":{"orderdetail":{"bak":"","crm_mco_billdocument_id":"CO2207010002","crm_mco_linkman":"","crm_mco_phone":"1399999999","crm_mco_address":" Tongliao village, Tongliao City ","crm_mco_seller":" Name of salesman ","crm_mco_operatedate":"2022-07-01 09:51:22","crm_mco_inoutspecies":"15","LinkMan":" Contact name ","hr_fty_name":" Delivery factory ","isCoupon":"0","ROW_NUMBER":"1","total_num":210,"total_tons":10.500000000000002,"total_money":36050},"ordergoods":{"CF00032-0004":{"data":{"productid":"CF00032-0004","appellation":" The product name ","crm_lco_amount":"40.0000","crm_lco_famount":"2.0000","crm_lco_summoney":"8700.0000","crm_lco_price":"217.50000000","crm_lco_fprice":"217.50000000","spec":"50kg\/ package ","productunitname":" package ","productType":"1","nature":" Tons of ","crm_lco_isgift":"","pstr8":" Product code ","ROW_NUMBER":"1"},"zeng_crm_lco_amount":"2.0000","zeng_crm_lco_famount":".1000","zeng_goods":" Product code ","productunitname":" package ","nature":" Tons of "},"CF00035-0004":{"data":{"productid":"CF00035-0004","appellation":" The product name ","crm_lco_amount":"120.0000","crm_lco_famount":"6.0000","crm_lco_summoney":"19800.0000","crm_lco_price":"165.00000000","crm_lco_fprice":"165.00000000","spec":"50kg\/ package ","productunitname":" package ","productType":"1","nature":" Tons of ","crm_lco_isgift":"","pstr8":" Product code 2","ROW_NUMBER":"3"},"zeng_crm_lco_amount":"6.0000","zeng_crm_lco_famount":".3000","zeng_goods":" Product code 2","productunitname":" package ","nature":" Tons of "},"CF00039-0004":{"data":{"productid":"CF00039-0004","appellation":" The product name 3","crm_lco_amount":"20.0000","crm_lco_famount":"1.0000","crm_lco_summoney":"3800.0000","crm_lco_price":"190.00000000","crm_lco_fprice":"190.00000000","spec":"50kg\/ package ","productunitname":" package ","productType":"1","nature":" Tons of ","crm_lco_isgift":"","pstr8":" Product code 3","ROW_NUMBER":"5"},"zeng_crm_lco_amount":"1.0000","zeng_crm_lco_famount":".0500","zeng_goods":" Product code ","productunitname":" package ","nature":" Tons of "},"CF00221-0001":{"data":{"productid":"CF00221-0001","appellation":" Product code ","crm_lco_amount":"20.0000","crm_lco_famount":"1.0000","crm_lco_summoney":"3750.0000","crm_lco_price":"187.50000000","crm_lco_fprice":"187.50000000","spec":"50kg\/ package ","productunitname":" package ","productType":"1","nature":" Tons of ","crm_lco_isgift":"","pstr8":" Product code ","ROW_NUMBER":"7"},"zeng_crm_lco_amount":"1.0000","zeng_crm_lco_famount":".0500","zeng_goods":" Product code ","productunitname":" package ","nature":" Tons of "}},"zenglist":[{"productid":"DG00001-0001","appellation":" Product code product code ","crm_lco_amount":"1.0000","crm_lco_famount":".0000","crm_lco_summoney":".0000","crm_lco_price":".00000000","crm_lco_fprice":".00000000","spec":"-","productunitname":" Pieces of ","productType":"6","nature":"0","crm_lco_isgift":" yes ","pstr8":" Product code product code ","ROW_NUMBER":"9"}]}}"""jsontext = jsontext.replace('\'','\"')order_detail = json.loads(jsontext)insert_data(order_detail)
What I want to achieve

Insert data into the database .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved