471,325 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

MySQLdb problem: inserting values

rhitam30111985
112 100+
hi all .. consider the following code: i basically need to build a table in the mysql database with two fields , country and office list...

Expand|Select|Wrap|Line Numbers
  1. import MySQLdb
  2. import sys
  3. import os
  4.  
  5. os.system('clear')
  6. try:
  7.     conn = MySQLdb.connect (host = "localhost",
  8.                         user = "root",
  9.                         passwd = "neptune",
  10.                         db = "mysql")
  11. except MySQLdb.Error, e:
  12.     print "Error %d: %s" % (e.args[0], e.args[1])
  13.     sys.exit (1)
  14. cursor = conn.cursor ()
  15. officelist={'india':['infosys','accenture','TCS','microsoft'],
  16.         'USA':['apple','microsoft','dupont','accenture']
  17.            }
  18. for item in officelist:
  19.     li=' '.join(officelist[item])
  20.     cursor.execute ("""
  21.            INSERT INTO office_list (country, offices)
  22.            VALUES
  23.             (item,li)
  24.                     """)
  25.  
  26.  
  27.  
but i am getting an error File "./check.py", line 21, in ?
cursor.execute ("""
File "/var/lib/python-support/python2.4/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/var/lib/python-support/python2.4/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column 'item' in 'field list'")

now it looks like i need to pass string values to the two fields country and offices in the cursor.execute() statement ... how do i pass the corresponding key,value from the dictionary officelist?
Sep 17 '07 #1
2 3913
rhitam30111985
112 100+
oops.. hey all.. never mind .. found solution.. :
Expand|Select|Wrap|Line Numbers
  1.     cursor.execute ("INSERT INTO office_list (country, offices)VALUES(%s,%s)",(item,li))
  2.  
but now new problem has come up: the values are getting truncated for the offices column.. how do i create an infinite length field ?
Sep 17 '07 #2
bartonc
6,596 Expert 4TB
oops.. hey all.. never mind .. found solution.. :
Expand|Select|Wrap|Line Numbers
  1.     cursor.execute ("INSERT INTO office_list (country, offices)VALUES(%s,%s)",(item,li))
  2.  
but now new problem has come up: the values are getting truncated for the offices column.. how do i create an infinite length field ?
Infinite, huh??? There are limits. As in the 4GB text type "LONGTEXT". For more a reasonable limit, use "TEXT" (64KB).

Of course, I'm speaking of the column types in your DB. Nothing to do with what you are doing in your program.
Sep 17 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Dave Harrison | last post: by
2 posts views Thread by Stephan Diehl | last post: by
3 posts views Thread by Achim Domma | last post: by
5 posts views Thread by Phillip | last post: by
1 post views Thread by Steve | last post: by
1 post views Thread by Yi Xing | last post: by
5 posts views Thread by Andrew Sackville-West | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.