Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9273537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:08:30+00:00 2026-06-18T16:08:30+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:08:31+00:00Added an answer on June 18, 2026 at 4:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble inserting a record into a MySQL database from python. This
I'm having trouble inserting image data into my database. I have a table called
I'm having trouble working this out for inserting input values into database. I have
I am having some trouble inserting an array into the sql database. my error
Hey guys, I'm having trouble in inserting my values into MySQL using PHP and
I'm having a bit of trouble inserting into a sqlite3 database with pdo. You'll
I'm having trouble inserting multiple records into an HTML5 Database. I verified the schema
I'm having trouble inserting rows into an SQL database. I want to turn an
I'm having trouble inserting data into my table. I have a list of stocks
I'm having trouble inserting data into a table. I have the following tables: track

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.