我有一个python脚本,它查询数据库并显示结果。我想将这些结果也存储在MySQL表中。如何修改脚本以查询数据库以及将结果存储在表中?
from django.conf import settings
from django.template import Template, Context
import MySQLdb
import sys
from tabulate import tabulate
from prettytable import PrettyTable
class license(object):
def __init__(self, arg):
super(license, self).__init__()
self.arg = arg
connection = MySQLdb.connect (host = "127.0.0.1", user = "root", passwd = "root", db = "edxapp1")
cursor = connection.cursor ()
cursor.execute ("""select shoppingcart_registrationcoderedemption.redeemed_by_id,auth_userprofile.name,auth_user.email,shoppingcart_registrationcoderedemption.registration_code_id,shoppingcart_courseregistrationcode.code,shoppingcart_courseregistrationcode.invoice_id
from shoppingcart_registrationcoderedemption inner join
shoppingcart_courseregistrationcode
on shoppingcart_registrationcoderedemption.registration_code_id = shoppingcart_courseregistrationcode.id
inner join auth_userprofile on shoppingcart_registrationcoderedemption.redeemed_by_id=auth_userprofile.user_id
inner join auth_user on auth_user.id=auth_userprofile.user_id order by redeemed_by_id""")
data = cursor.fetchall ()
t = PrettyTable(['USER ID','NAME', 'EMAIL', 'LICENSE ID', 'CODE', 'INVOICE ID'])
for row in data :
t.add_row([row[0] , row[1] , row[2], row[3], row[4], row[5]])
print t
cursor.close ()
谢谢!