[Tutor] sqlite3 Python25 parameter binding problem withUPDATEplease help

Alan Gauld alan.gauld at btinternet.com
Wed Dec 3 01:02:14 CET 2008


"aivars" <aivars868 at gmail.com> wrote

> Finally I managed to get it working!
>
> I had to remove [year] braces from the second argument - year.

You really need to do some reading on basicv Python before
you go any further, otherwise you will never understand what
you are doing.

That is what I told you was wrong in the first place - you can't
pass a list of values (even of one value)  into a database field.
But you obviously didn't realize that putting square brackets
(not braces which are {} and signify a dictionary) indicates
a list.

> reason I used [] was the posts we exchanged recently (7th of 
> November)
> about CGI script where it was recommended by Kent that I put [] 
> around
> string argument:

But that was an entirely different scenario, again you need to do some
basic level reading so that you understand what we are telling you.
Blindly using "poke and hope" techniques leads to an excercise
in frustration for you and us both.

> The second argument to execute() is a *sequence* of parameter 
> values.
> A string is a sequence of characters, so by passing a plain string 
> you
> are saying that each character of the string is a parameter to the
> SQL.
>
> Try adding braces around the parameter to make  list:
> cur.execute("insert into test (name) values (?)", [sPath])

Notice, Kent told you to "make a list" by putting[] around
your string. But in that case the arument was looking for a sequence
(eg list) of values. In your case you were trying to insert a list of 
values
into a single field which you can't do in normal SQL

> Now the year argument is string so I thought I should do the same
> again but it is not working.

Yes but it is a single string so you need to pass a single string.
In the previous case the SQL was looking for a list of arguments
but you passed a single string so the execute interpreted that
as a list of letters.

You must ensure that the arguments in the execute match
what the SQL is expecting. And that usually means both matching
what you defined the database to be and the nature of the operation.

> The reason is of course is that I am new to python and there are so
> many things to learn so the question really is why in this case 
> there
> were no [] or () necessary?

See above, but you really need to take the time out to understand
the basics of Python.

Try reading my  tutorial, in particular the "raw materials"  topic
which covers the different Python data types. Then try my database
topic near the end. But ideally just take a few hours - literally an
afternoon will do - to go through any beginners tutorial. They will
all cover these elementary issues.


>>>> Perhaps but I think it is the list parameter that it doesn't 
>>>> like.
>>>> Unless I misunderstand the syntax.
>>>>
>>>> --
>>>> Alan Gauld
>>>> Author of the Learn to Program web site
>>>> http://www.freenetpages.co.uk/hp/alan.gauld


Alan G. 




More information about the Tutor mailing list