473,241 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 4045
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

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

Similar topics

0
by: Dave Harrison | last post by:
Hi all, got a problem combinging mx and MySQLdb, when I build and install both for my Python2.1 install on a Solaris 9 box I can import mx fine, but importing MySQLdb causing python to core dump. ...
2
by: Stephan Diehl | last post by:
Since using Python2.3, MySQLdb doesn't work anymore correctly. Inserting data fails with "IndexError: list index out of range". Since the MySQLdb website states, that it runs up to version 2.2, I...
3
by: Achim Domma | last post by:
Hi, I'm using MySQLdb with Python 2.3 on windows. Querying a database seems to work fine, but inserting does not. If I try something like this: cursor.execute("insert into Webpages (Url)...
5
by: Phillip | last post by:
Hi. I hate the way one has to jump through countless hoops to put data in a db and get it out again. The straightforward MySQLdb Interface requireing this SQL stuff being a point in case (against...
1
by: Steve | last post by:
Darwin steve.local 8.3.0 Darwin Kernel Version 8.3.0: Mon Oct 3 20:04:04 PDT 2005; root:xnu-792.6.22.obj~2/RELEASE_PPC Power Macintosh powerpc MacOSX 10.4.3 mysql Ver 14.7 Distrib 4.1.14, for...
1
by: shearichard | last post by:
Hi - I have written some python to insert a row into a table using MySQLDB. I have never before written SQL/Python using embedded parameters in the SQL and I'm having some difficulties. Could...
1
by: Yi Xing | last post by:
Hi, I met the following error when I tried to install MySQLdb. I had no problem installing numarray, Numeric, Rpy, etc. Does anyone know what's the problem? Thanks! running install running...
5
by: Andrew Sackville-West | last post by:
Hi list, I've tried, lots of interpreter testing and google grepping to figure this out and I think I'm missing something fundamental. I have an ascii data dump from a POS system that has 131...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.