首先,我在mysql数据库的表“a”中有一个列,其名称为dataindex,从0到19.我编写的函数“X”将返回值“Z”,从o到19并基于在这个函数返回的值上,我需要从数据库中提取数据。假设函数“X”返回值5,那么我需要来自数据库中我的表“a”的索引为5的a,b,c,d列的数据。
获取此数据后,我需要将获取的数据写入另一个表“b”。到目前为止,我已编写了用于从数据库中提取所需数据的代码,我需要将此数据插入另一个表中。我该怎么做?函数插入记录不是将数据写入table_b。
def getdata():
cnxn = dbConnect()
cursor = cnxn.cursor()
final = num_of_work_days()
qry = "SELECT `a`, `b`, `c`, `d` FROM `table_a` WHERE `dataindex`=%s"
cursor.execute(qry, (Z,))
result = cursor.fetchall()
cnxn.commit()
return result
print(getdata())
def insertrecords():
cnxn = dbConnect()
cursor = cnxn.cursor()
result = getdata()
sql ="""INSERT INTO table_b (a, b, c, d) VALUES (%s, %s, %s, %s)"""
cursor.execute(sql, (result))
cnxn.commit()
print(insertrecords())