[Tutor] School Boy Error - Update

Liam Clarke ml.cyresse at gmail.com
Mon Apr 17 09:21:35 CEST 2006


Dude, you've still got your trailing comma in stat. Get rid of it.

If I jump into MySQL, here's an example -

create table a (a int, b int); OK
insert into a values (5, 4); OK
insert into a values(5, 4,); Syntax error

----

Try something like this - it's more scalable too.

def generateSQL(data):
    stat = "Insert into cost_grid values ("
    num_of_q = len(data)
    qs = list(num_of_q * "?")
    string_qs = ", ".join(qs)
    final_string = stat + string_qs + ")"
    return final_string

And you can generate parameterised statements with -

stat = generateSQL(listy[0])

But yeah. Apologies for the caps, but DROP THE TRAILING COMMA. May I
also recommend downloading SQLite http://www.sqlite.org/download.html
and playing in that to confirm your SQL, because that comma is the
problem.

i.e. stat should be

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

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

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

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

On 4/16/06, John CORRY <john.corry at ntlworld.com> wrote:
>
>
>
> Hi,
>
>
>
> I have taken on board the advice in relation to the cvs module and setting
> the list to a tuple.  I am now using the following code and getting a
> different error.  I think it is a small step forward?
>
>
>
> import string, re
>
> path = "c:/test/import.csv"
>
> listy = []
>
> import csv
>
> reader = csv.reader(open(path,"rb"))
>
> for row in reader:
>
>     listy.append(tuple(row))
>
>
>
> sql_list = listy[0]
>
>
>
>
>
> stat = """Insert into cost_grid values
> (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)"""
>
> t = 5000
>
> d = "PF2"
>
> b = 91.4
>
> a = 95.00
>
> print stat
>
> print sql_list
>
> import mx.ODBC
>
> import mx.ODBC.Windows
>
> db = mx.ODBC.Windows.DriverConnect('DSN=vfp')
>
> c = db.cursor()
>
> c.execute(stat, sql_list)
>
>
>
> db.commit()
>
> c.close()
>
>
>
>
>
> I now get the following ouput + error:
>
>
>
> Insert into cost_grid values
> (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
>
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)
>
> ('961', 'TM BLIND', 'RO', 'PF1', 'Plain Finish Range One', '91.4', '',
> '45.7', '10', '20', '0', '24', '61', '8.69', '20', '0', '27', '81.3',
> '11.03', '25', '0', '34', '101.6', '12.36', '28', '0', '38', '121.9',
> '14.36', '32', '0', '44', '142.2', '16.7', '37', '0', '51', '162.6',
> '18.71', '41', '0', '58', '182.9', '20.72', '45', '0', '64', '198.1',
> '22.71', '49', '0', '70', '223.5', '27.39', '60', '0', '84', '243.8',
> '30.07', '66', '0', '92', '274.3', '34.41', '76', '0', '105', '304.8',
> '37.42', '82', '0', '115', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
> '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
> '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')
>
> Traceback (most recent call last):
>
>   File "C:\Python24\Lib\site-packages\databasemanager.py",
> line 35, in ?
>
>     c.execute(stat, sql_list)
>
> ProgrammingError: ('37000', 200, '[Microsoft][ODBC Visual FoxPro
> Driver]Syntax error.', 4347)
>
>
>
> I assume this error is telling me that the database does not like the syntax
> of the data I am importing.  The data that I am importing is actually data
> that I exported, so I know that it is in the right order and the right
> amount.  The only thing that I can think of is that, the database requires
> floats for the numbers instead of strings.  I assume that the numbers in
> listy[0] are now strings.  Am I now going to have to throw listy[0] through
> a function to make the numbers into floats or is there another way to do
> this?
>
>
>
> Am I on the right track?
>
>
>
> Thanks,
>
>
>
> John.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


More information about the Tutor mailing list