473,657 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQLdb problem: inserting values

rhitam30111985
112 New Member
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.errorhandl er(self, exc, value)
File "/var/lib/python-support/python2.4/MySQLdb/connections.py" , line 35, in defaulterrorhan dler
raise errorclass, errorvalue
_mysql_exceptio ns.OperationalE rror: (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 4076
rhitam30111985
112 New Member
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 Recognized Expert Expert
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
2096
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. I am using egenenix 2.0.4 and MySQLdb 0.9.2. I have also replaced the -shared flag in the Makefile is /usr/local/lib/Python2.1/config with -G (a recommended solaris change to let the build of the modules work in the first place) -> notably I also...
2
1767
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 definatelly shouldn't complain :-) Is there somebody whose MySQLdb is running with 2.3? MySQLdb 0.9.2 Python 2.3 Linux
3
15366
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) values (?)", (url,)) I get the following Error: File "C:\Python23\Lib\site-packages\MySQLdb\cursors.py", line 95, in
5
1655
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 SQL and those RDBs that is). Since other programms have to access this data I'm forced to use a classical DB and actually have managed to set up Mysql and a presumably working connection from Python to it. I've gotten so far as to avoid errors...
1
2735
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 apple-darwin8.2.0 (powerpc) using readline 4.3 runing the software gives me steve:~/MySQL-python-1.2.0 steve$ python setup.py build
1
7474
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 someone point me in the right direction please ? The python looks like this : import MySQLdb import MySQLdb.cursors conn = MySQLdb.Connect(host='localhost', user='abc,passwd='def',
1
702
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 build running build_py creating build
5
1316
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 fields in a single column in a flat file. I can easily open the file, read in the data and assemble it into various formats. okay. what I *want* to do is insert each of these fields into a mysql database that has 132 columns that correspond to...
2
3075
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 section: Discography --------------------- DiscID
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.