[Tutor] How to import python dictionary into MySQL table?

Sean Carolan scarolan at gmail.com
Fri Dec 17 04:39:57 CET 2010


> I have a database with a table called "systems" that contains an
> auto-increment id field, as well as fields for each of the keys in
> mydata.keys().  But I can't seem to get the syntax to import
> mydata.values() into the table.  I think the problem may be that some
> of the items in my list are dictionaries or lists...
>
> What would be the quickest and most straightforward way to do this?

I got this working in case anyone else comes across the same problem.
This function will pull cobbler *.json data into a MySQL database
table.  The function assumes that you already have a database set up,
and that you are dumping the data into the "systems" table:

def importJSON(targetfile):
    '''
    Imports JSON data from targetfile into MySQL database table.
    '''
    value_list = []
    rawdata = json.load(open(targetfile))
    for key in rawdata.keys():
        strvalue = str(rawdata[key]).replace("'",'"')
        value_list.append(strvalue)
    valtuple = tuple(value_list)
    sql = "INSERT INTO systems (comment, kickstart,
name_servers_search, ks_meta, kernel_options_post, image,
redhat_management_key, power_type, power_user, kernel_options, vi
rt_file_size, mtime, template_files, gateway, uid, virt_cpus,
hostname, virt_type, mgmt_classes, power_pass, netboot_enabled,
profile, virt_bridge, parent, virt_path, interfaces, power_address,
name_servers, name, owners, ctime, virt_ram, power_id, random_id,
server, redhat_management_server, depth) VALUES (%s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
    cursor.execute(sql, valtuple)


More information about the Tutor mailing list