School Boy Error

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

School Boy Error

John Corry-3

Thanks Brian for the help on the last one.  I couldn’t see the wood for the trees.  I have a tougher one for you.

 

I am reading in a CSV file.  Each line represents a line that I want to upload into my database.  I am trying to upload the first line in the file to get myself started.  The code is below:-

 

import string, re

path = "c:/test/import.csv"

listy = []

input = file(path, "r")

for line in input.readlines():

    line = line.split(",")

    listy.append(line)

   

print listy[-1]

 

stat = """Insert into cost_grid values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,

?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,

?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,

?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)"""

t = 5000

d = "PF2"

b = 91.4

a = 95.00

 

import mx.ODBC

import mx.ODBC.Windows

db = mx.ODBC.Windows.DriverConnect('DSN=vfp')

c = db.cursor()

c.execute(stat, listy[-1])

  

db.commit()

c.close()

 

I get the following error:

 

TypeError: parameters must be a list of tuples

 

Any suggestions would be greatly appreciated.

 

Thanks,

 

John.


_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: School Boy Error

Brian Gustin
would be helpful to paste the entire error information that python gives ..
including the lines before and after.
When pytho sets an error , it will tell you *exactly* where the error is
at (or it should) I do all my development work on Linux, havent worked
with ODBC databases, but the reference to a list of tuples , almost
makes me thing the SQL needs to be a full insert:
(insert into tablename (field1,field2.....) values (value1,value2....) )




John CORRY wrote:

> Thanks Brian for the help on the last one.  I couldn’t see the wood for
> the trees.  I have a tougher one for you.
>
>  
>
> I am reading in a CSV file.  Each line represents a line that I want to
> upload into my database.  I am trying to upload the first line in the
> file to get myself started.  The code is below:-
>
>  
>
> import string, re
>
> path = "c:/test/import.csv"
>
> listy = []
>
> input = file(path, "r")
>
> for line in input.readlines():
>
>     line = line.split(",")
>
>     listy.append(line)
>
>    
>
> print listy[-1]
>
>  
>
> stat = """Insert into cost_grid values
> (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)"""
>
> t = 5000
>
> d = "PF2"
>
> b = 91.4
>
> a = 95.00
>
>  
>
> import mx.ODBC
>
> import mx.ODBC.Windows
>
> db = mx.ODBC.Windows.DriverConnect('DSN=vfp')
>
> c = db.cursor()
>
> c.execute(stat, listy[-1])
>
>  
>
> db.commit()
>
> c.close()
>
>  
>
> I get the following error:
>
>  
>
> TypeError: parameters must be a list of tuples
>
>  
>
> Any suggestions would be greatly appreciated.
>
>  
>
> Thanks,
>
>  
>
> John.
>
> !DSPAM:444178de174651249418164!
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/tutor
>
>
> !DSPAM:444178de174651249418164!
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: School Boy Error

Liam Clarke
In reply to this post by John Corry-3
Hi John,

Listy will be a list of lists, and the DBAPI specifies tuples. So
either change this -

listy.append(line)

to

listy.append(tuple(line))

or stick a list comprehension at the end if you need to mung anything
in listy before passing it in:

listy = [ tuple(item) for item in listy]

Regards,

Liam Clarke

On 4/16/06, John CORRY <[hidden email]> wrote:

>
>
>
> Thanks Brian for the help on the last one.  I couldn't see the wood for the
> trees.  I have a tougher one for you.
>
>
>
> I am reading in a CSV file.  Each line represents a line that I want to
> upload into my database.  I am trying to upload the first line in the file
> to get myself started.  The code is below:-
>
>
>
> import string, re
>
> path = "c:/test/import.csv"
>
> listy = []
>
> input = file(path, "r")
>
> for line in input.readlines():
>
>     line = line.split(",")
>
>     listy.append(line)
>
>
>
> print listy[-1]
>
>
>
> stat = """Insert into cost_grid values
> (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)"""
>
> t = 5000
>
> d = "PF2"
>
> b = 91.4
>
> a = 95.00
>
>
>
> import mx.ODBC
>
> import mx.ODBC.Windows
>
> db = mx.ODBC.Windows.DriverConnect('DSN=vfp')
>
> c = db.cursor()
>
> c.execute(stat, listy[-1])
>
>
>
> db.commit()
>
> c.close()
>
>
>
> I get the following error:
>
>
>
> TypeError: parameters must be a list of tuples
>
>
>
> Any suggestions would be greatly appreciated.
>
>
>
> Thanks,
>
>
>
> John.
> _______________________________________________
> Tutor maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: School Boy Error

Liam Clarke
Wait, I have put you wrong there.

Can you please copy and paste here the output of print liney[-1]

Thanks,

Liam

On 4/16/06, Liam Clarke <[hidden email]> wrote:

> Hi John,
>
> Listy will be a list of lists, and the DBAPI specifies tuples. So
> either change this -
>
> listy.append(line)
>
> to
>
> listy.append(tuple(line))
>
> or stick a list comprehension at the end if you need to mung anything
> in listy before passing it in:
>
> listy = [ tuple(item) for item in listy]
>
> Regards,
>
> Liam Clarke
>
> On 4/16/06, John CORRY <[hidden email]> wrote:
> >
> >
> >
> > Thanks Brian for the help on the last one.  I couldn't see the wood for the
> > trees.  I have a tougher one for you.
> >
> >
> >
> > I am reading in a CSV file.  Each line represents a line that I want to
> > upload into my database.  I am trying to upload the first line in the file
> > to get myself started.  The code is below:-
> >
> >
> >
> > import string, re
> >
> > path = "c:/test/import.csv"
> >
> > listy = []
> >
> > input = file(path, "r")
> >
> > for line in input.readlines():
> >
> >     line = line.split(",")
> >
> >     listy.append(line)
> >
> >
> >
> > print listy[-1]
> >
> >
> >
> > stat = """Insert into cost_grid values
> > (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
> >
> > ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
> >
> > ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
> >
> > ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)"""
> >
> > t = 5000
> >
> > d = "PF2"
> >
> > b = 91.4
> >
> > a = 95.00
> >
> >
> >
> > import mx.ODBC
> >
> > import mx.ODBC.Windows
> >
> > db = mx.ODBC.Windows.DriverConnect('DSN=vfp')
> >
> > c = db.cursor()
> >
> > c.execute(stat, listy[-1])
> >
> >
> >
> > db.commit()
> >
> > c.close()
> >
> >
> >
> > I get the following error:
> >
> >
> >
> > TypeError: parameters must be a list of tuples
> >
> >
> >
> > Any suggestions would be greatly appreciated.
> >
> >
> >
> > Thanks,
> >
> >
> >
> > John.
> > _______________________________________________
> > Tutor maillist  -  [hidden email]
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
>
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: School Boy Error

David Rock
In reply to this post by John Corry-3
* John CORRY <[hidden email]> [2006-04-15 23:48]:
> Thanks Brian for the help on the last one.  I couldn't see the wood for
> the trees.  I have a tougher one for you.
>  
> I am reading in a CSV file.  Each line represents a line that I want to
> upload into my database.  I am trying to upload the first line in the
> file to get myself started.  The code is below:-

You may want to check out the csv module to aid in any odd data input,
too.
http://docs.python.org/lib/module-csv.html

--
David Rock
[hidden email]
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor