2

I manipulated some data from MySQL and the resulting dictionary "data" (print data) displays something like this :

{'1': ['1', 'K', abc, 'xyz', None, None, None, datetime.date(2009, 6, 18)],
 '2': ['2', 'K', efg, 'xyz', None, None, None, None],
 '3': ['3', 'K', ijk, 'xyz', None, None, None, datetime.date(2010, 2, 5, 16, 31, 2)]}

How do I create a table and insert these values in a MySQL table? In other words, how do I dump them to MySQL or CSV? Not sure how to deal with datetime.date and None values. Any help is appreciated.

2
  • 1
    Are you missing one None? (use a class...) Commented Apr 12, 2010 at 16:05
  • Yep, updated! THanks. This is just sample data. Thanks Ken. Commented Apr 12, 2010 at 16:36

3 Answers 3

1

Here is some basic code to create a MySQL database, and insert some data.

import MySQLdb
import datetime

THEHOST="localhost"
THEUSER="user"
THEPASSWD="passwd"
THEDB="database"

connection=MySQLdb.connect(
    host=THEHOST,user=THEUSER,passwd=THEPASSWD,db=THEDB)
cursor=connection.cursor()

abc,efg,ijk=1,2,3

data={'1': ['1', 'K', abc, 'xyz', None, None, None, datetime.date(2009, 6, 18)],
      '2': ['2', 'K', efg, 'xyz', None, None, None, None],
      '3': ['3', 'K', ijk, 'xyz', None, None, None,
            datetime.datetime(2010, 2, 5, 16, 31, 2)]}

sql='''\
CREATE TABLE IF NOT EXISTS temp (id int auto_increment primary key,
    field1 varchar(8),
    field2 int,
    field3 varchar(8),
    field4 bool,
    field5 varchar(8),
    field6 varchar(8),
    field7 datetime )'''

cursor.execute(sql)

sql='''\
INSERT INTO temp (id, field1, field2, field3, field4, field5, field6, field7)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
'''
cursor.executemany(sql, data.values())
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the solution :) I keep getting this error though : TypeError: not all arguments converted during string formatting. I used this : sql='''\ INSERT INTO delUseThisTable (key, primary) VALUES (%s, %s) ''' cursor.executemany(sql, data.values()) I have 78 columns in this table. I tried with 2 columns for a start and it fails. Ideas? Thank u!
@NJTechie: Perhaps try print data.values(). It should be a list of 2-element lists, such as [[1,'K'],[2,'L']]. The inner lists must have exactly 2 elements. Otherwise, you may get the error TypeError: not all arguments converted...
OMG, sir you are a genius :) I didn't know we have to insert all of them at once rather than just 2 columns! Definitely learnt a thing or 2! Thank you sooo much!
1

datetime.date matches DATE fields, and None becomes NULL. Use .executemany() in conjunction with dict.values() in order to perform the INSERT.

Comments

1

Most dbapi-compliant connectors to MySQL will automatically convert Python's None to SQL's NULL and Python's datetime objects to SQL TIMESTAMPs.

Really, you just need to open a connection to your database, get a cursor, and iterate over the dictionary doing the insert. Without knowing your schema, it's impossible to give you sample code, though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.