Re: [DB-SIG] mysql string length?
by Andy Todd other posts by this author
Apr 17 2006 2:46AM messages near this date
Re: [DB-SIG] mysql string length?
|
Re: [DB-SIG] mysql string length?
engelbert.gruber@[...].at wrote:
> On Sun, 16 Apr 2006, Andy Todd wrote:
>
> > Lukasz Szybalski wrote:
> >> On Sat, 2006-04-15 at 12:01 +0000, Andrew Chambers wrote:
> >>> * Lukasz Szybalski <szybalski@[...].com> [2006-04-14 12:00:09 -0500]:
> >>>
> >>>> INSERT INTO table_x( body)VALUES( '%s')" % (body)
> >>>>
> >>>> this body is a string that varies in size. I keep getting an error if
> >>>> the size of body is longer then 255, and if its smaller everything
> >>>> goes smooth. Is this syntax correct? should '%s' be something else?
> >>> What is the datatype of body? It sounds like it is CHAR(255). Can you
> >>> change this to be TEXT?
> >>>
> >> Yes it is text.
> >> mysql> describe table_x;
> >> +----------+---------+------+-----+---------+-------+
> >> | Field | Type | Null | Key | Default | Extra |
> >> +----------+---------+------+-----+---------+-------+
> >> | id | int(11) | | PRI | 0 | |
> >> | body | text | YES | | NULL | |
> >> +----------+---------+------+-----+---------+-------+
> >>
> >> Would some kind of characters that are passed in had something to do
> >> with the error, maybe EOF or something similar?
> >>
> >> The string that is passed in is from xml node. For debugging i made it
> >> return str(body), but that didn't change anything.
> >>
> >> Are there any other database/%s related requirements?
>
> not the way you pass it (as i understand it python first does "%"
> replacement and then passes the string.
>
> > My observation is that with MySQLdb %s is a parameter substitution value
> > not a string substitution indicator.
> >
> > What happens when you try something like this;
> >
> >>>> stmt = "INSERT INTO table_x (body) VALUES (%s)" # [1]
> >>>> cursor.execute(stmt, (body,))
> > [1] note that there are no quote marks around the %s
>
> this works here (debian testing, python 2.3.5, 4.0.21)
> ::
>
> import MySQLdb
> """
> create table table_x (
> id int auto_increment primary key,
> body text
> )
> """
> db = MySQLdb.connect(passwd="",db="test")
> c = db.cursor()
> body = "0....5...."*28
> sql = "INSERT INTO table_x (body) VALUES('%s')"
> c.execute( sql % (body))
> c.execute("select body from table_x")
> for row in c.fetchall():
> print len(row[0])
>
> i wont bet on fetching 1Mb.
>
> cheers
You've just repeated the original poster's code. As I suggested the
problem is that he's using string substitution when he should be using
parameter substitution. In these cases %s means two completely different
things.
Please read the sections on 'paramstyle' and the cursor '.execute'
method in the DB-API 2.0 definition [1].
Regards,
Andy
[1] http://www.python.org/dev/peps/pep-0249/
--
--------------------------------------------------------------------------------
From the desk of Andrew J Todd esq - http://www.halfcooked.com/
_______________________________________________
DB-SIG maillist - DB-SIG@[...].org
http://mail.python.org/mailman/listinfo/db-sig
Thread:
Lukasz Szybalski
Andrew Chambers
Lukasz Szybalski
Andy Todd
Engelbert Gruber
Andy Todd
Engelbert Gruber
Lukasz Szybalski
Lukasz Szybalski
Andy Todd
Lukasz Szybalski
M.-A. Lemburg
Ian Bicking
Gerhard Häring
Ian Bicking
Carsten Haese
Gerhard Häring
Carsten Haese
Carsten Haese
Python
Engelbert Gruber
Andy Dustman
|