简体   繁体   中英

Insert a row into a MySQL table when one of the elements is a Python dictionary

I am having trouble inserting an element into a MySQL database.

Here is the code I have:

#!/usr/bin/python
# -*- coding: utf-8 -*-

myData = [ { u'my_text' : {u'id': u'1', u'description' : u'described' }, u'my_id' : u'1' } ]

import MySQLdb as mdb
con = None
con = mdb.connect('localhost', 'abc', 'def', 'ghi');
cur = con.cursor()
con.set_character_set('utf8')
cur.execute('SET NAMES utf8;')
cur.execute('SET CHARACTER SET utf8;')
cur.execute('SET character_set_connection=utf8;')

sql = "INSERT IGNORE INTO MyTable ( 'my_id', 'my_text' ) VALUES ( %(my_id)s, %(my_text)s );"
cur.executemany(sql, myData)
con.commit()

if con: con.close()

My database is created with this:

CREATE TABLE MyTable(
    'my_id' INT(10) unsigned NOT NULL,
    'my_text' TEXT
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

As you can see from the python script one element of my list is a dictionary and it seems to be this that is stopping the insertion into the MySQL database but as I have only started using MySQL in the last few days I may be wrong.

If I make my_text within myData a simple text phrase such as the following everything works fine and the insertion into the database table works fine:

myData = [ { u'my_text' : u'something simple', u'my_id' : u'1' } ]

I want to be able to use this:

myData = [ { u'my_text' : {u'id': u'1', u'description' : u'described' }, u'my_id' : u'1' } ]

What am I doing wrong?

You have at least two choices.

  1. Change your table schema:

     CREATE TABLE MyTable( 'my_id' INT(10) unsigned NOT NULL, 'id' INT(10), 'description' TEXT ) ENGINE=MyISAM DEFAULT CHARSET=utf8 sql = "INSERT IGNORE INTO MyTable ( 'my_id', 'id', 'description' ) VALUES ( %s, %s, %s )" myArg = [(dct['my_id'], dct['my_text']['id'], dct['my_text']['description']) for dct in myData] cur.executemany(sql, myArg) 
  2. or change the arg passed to cur.executemany :

     myData = [ { u'my_text' : {u'id': u'1', u'description' : u'described' }, u'my_id' : u'1' } ] myArg = [ {'my_txt' : str(dct['my_text']), 'my_id' : dct['my_id']} for dct in myData ] cur.executemany(sql, myArg) 

The advantage of changing the table schema is that you can now select based on id , so the data is richer.

But if you have no need to separate the id from the description, then the second way will work without having to change the table schema.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM