473,394 Members | 1,481 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,394 software developers and data experts.

MySQLdb, lots of columns and newb-ness

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 the 131 fields in the ascii file (plus one
for the date).

I can successfully connect to mysql and do stuff to my tables my
specific problem is how to efficiently put those 132 fields into the
thing. All I have been able to figure out is really ugly stuff like:
build the mysql statement out of various pieces with appropriate
commas and quote included. stuff like (not tested)

for field in f.read():
row+=field[:-2]+", "

stmt="insert into daily values "+row")"
cursor.execute(stmt)

(the slice is to kill a cr/lf on each one)

that seems really kludgey to me.

I've also tried building tuples and lists and then using this

cursor.execute("insert into daily values (%s)", values)

with no luck. it appears to me that I have to put in all 132 '%s' in
order to make that work and that just seems stupid.

I suppose I could build a list of the column names:

columns=('Asales', 'Bsales', 'Csales' ...)

and bring in the data as a list and then

for col in range(len(columns)):
cursor.execute("insert into daily (%s) values (%s)",
(columns[col], data[col]))

but again, that doesn't seem too pythonic.

any suggestions are greatly appreciated.

A

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFiIMsaIeIEqwil4YRArkGAKCHY6UG/NT+9fFTo61/GfVVuPJN3wCbB8mh
pW4FBdDs7FfUD0lEpIm8A4M=
=kAWM
-----END PGP SIGNATURE-----

Dec 20 '06 #1
5 1306
Andrew Sackville-West wrote:
>
I can successfully connect to mysql and do stuff to my tables my
specific problem is how to efficiently put those 132 fields into the
thing. All I have been able to figure out is really ugly stuff like:
build the mysql statement out of various pieces with appropriate
commas and quote included. stuff like (not tested)
I just started looking into Python myself, so someone can probably
clean this up or suggest a better way, but this may work:
import MySQLdb

fields = ["field1\r\n","field2\r\n","field3\r\n"]

def escapeAndQuote(x):
return "\"%s\"" % MySQLdb.escape_string(x)

values = ", ".join([escapeAndQuote(f[:-2]) for f in fields])
q = "insert into daily values(%s)" % values
In testing I got:
>>fields = ["field1\r\n","field2\r\n","field3\r\n"]
values = ", ".join([escapeAndQuote(f[:-2]) for f in fields])
values
'"field1", "field2", "field3"'
>>q = "insert into daily values(%s)" % values
'insert into daily values("field1", "field2", "field3")'

Todd

Dec 20 '06 #2
On Tue, Dec 19, 2006 at 07:34:58PM -0800, Todd Neal wrote:
Andrew Sackville-West wrote:

I can successfully connect to mysql and do stuff to my tables my
specific problem is how to efficiently put those 132 fields into the
thing. All I have been able to figure out is really ugly stuff like:
build the mysql statement out of various pieces with appropriate
commas and quote included. stuff like (not tested)
I just started looking into Python myself, so someone can probably
clean this up or suggest a better way, but this may work:
okay, let me run through this and see if I understand:


import MySQLdb

fields = ["field1\r\n","field2\r\n","field3\r\n"]
build a list of data fields to be inserted (whatever method)

def escapeAndQuote(x):
return "\"%s\"" % MySQLdb.escape_string(x)
not at the right machine to read up on this but obviously it cleans up
the strings and inserts the quotes around each field.

values = ", ".join([escapeAndQuote(f[:-2]) for f in fields])
crap. I knew about .join. that was really the part I was missing.
q = "insert into daily values(%s)" % values
make the query statement.

In testing I got:
>fields = ["field1\r\n","field2\r\n","field3\r\n"]
values = ", ".join([escapeAndQuote(f[:-2]) for f in fields])
values
'"field1", "field2", "field3"'
>q = "insert into daily values(%s)" % values
'insert into daily values("field1", "field2", "field3")'
cool! thanks Todd.

A



Todd

--
http://mail.python.org/mailman/listinfo/python-list
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFiMgAaIeIEqwil4YRAiDNAKDmGHmUZCR/9UXARWcL1VTvs/jc8ACgk8FV
WKjfs0lVHjDhxf/EUHv0uLY=
=La5Z
-----END PGP SIGNATURE-----

Dec 20 '06 #3
Andrew Sackville-West schrieb:
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 the 131 fields in the ascii file (plus one
for the date).

I can successfully connect to mysql and do stuff to my tables my
specific problem is how to efficiently put those 132 fields into the
thing. All I have been able to figure out is really ugly stuff like:
build the mysql statement out of various pieces with appropriate
commas and quote included. stuff like (not tested)

Haven't tested it, but maybe
http://dev.mysql.com/doc/refman/5.0/en/load-data.html is your friend.
Dec 20 '06 #4
Ant


On Dec 20, 5:20 am, Andrew Sackville-West <and...@farwestbilliards.com>
wrote:
>>values = ", ".join([escapeAndQuote(f[:-2]) for f in fields])
Obviously this is the appropriate choice since this is a database app.
In general the strip() group of string methods do what you want in a
safe way - assuming you don't care about whitespace:
>>s = " test \r\n"
s.strip()
'test'
>>s.rstrip()
' test'
>>s.lstrip()
'test \r\n'

If you are concerned about whitespace:
>>s.strip("\n\r")
' test '

strips any \n's or \r's from the ends of the line.

This way it doesn't matter what your line endings are - you won't be
surprised by missing characters if the data dump changes for any
reason.

Dec 20 '06 #5
On Wed, Dec 20, 2006 at 07:00:38AM -0800, Ant wrote:


On Dec 20, 5:20 am, Andrew Sackville-West <and...@farwestbilliards.com>
wrote:
>values = ", ".join([escapeAndQuote(f[:-2]) for f in fields])
Obviously this is the appropriate choice since this is a database app.
In general the strip() group of string methods do what you want in a
safe way - assuming you don't care about whitespace:
>s = " test \r\n"
s.strip()
'test'
perfect!

[...]

This way it doesn't matter what your line endings are - you won't be
surprised by missing characters if the data dump changes for any
reason.
well, no great chance of the data dump changing, but its a good
point.

thanks
A

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFiYZFaIeIEqwil4YRAmzNAJ9oMpauskfPkli0QDZubD fIAgtF7ACeNNzd
g+++qAbfhLSyfwt+suSvc94=
=rBBV
-----END PGP SIGNATURE-----

Dec 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

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. ...
1
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to...
2
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works,...
2
by: m0226065 | last post by:
Ok so I have a mySQL server and mySQLdb as an interface (do I us this term correct here?) to this server from python now I have a game: I want to read a file into the db I want other data to...
1
by: Achim Domma (Procoders) | last post by:
Hi, I try to write unicode strings to a MySQL database via MySQLdb. According to the documentation I should pass 'utf-8' as keyword parameter to the connect method. But then I get the following...
2
by: yaffa | last post by:
dear group, i have a python script that scrapes contents from an html file and i would like to have the script write values to a mysql db. do you recommend i go with the sqlobject or the...
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: 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...
2
by: Nikhil | last post by:
I am using the MySQLdb python module. I have a table named 'testing' with few columns, under the 'test' database, what is hosted on a remote mysql server. I want to run the following query to...
0
by: Edwin.Madari | last post by:
db module properly formats arguments, if input arguments are separated fromtable_name and colum_names..... columns = ('tID', 'tNote') table_name = 'tmp' sql = 'select %s from %s where tID=:1' %...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.