python读取数据库之给变量_如何使用变量将python中的数据插入到mysql数据库中?...

在向查询中插入数据时,如何直接使用变量代替字符串?import pymysql

import json

# Connect to the database

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

user='test',

password='br',

db='testing')

cur = conn.cursor()

json_data = json.loads(open("res.json").read())

# currently this is the query I want to execute

query = """INSERT INTO RF.candidate (

first_name, last_name, phone, email,

social, website

) VALUES (

'David', 'N', '["+1 917 547 7813", "+1 917 547 7823"]',

'["testing@gmail.com"]', '["fb.com/rtr"]', '["google.com", "sris.com"]',

'{"Degree": {"MBA": {"Majors": [""]}}}',

);"""

cur.execute(query)

# datatypes:

# first_name : varchar

# last_name : varchar

# phone : json

# email : json

# social : json

# website: json

# education: json

电子邮件地址数据位于JSON_DATA变量中,print(json_data['email'])

# returns

["testing@gmail.com"]

email_array = json.dumps(json_data['email'])

现在我想在sql查询email列中传递变量email_array,而不是像上面那样在查询变量中手动编写json列表?

我怎么用Python做呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值