Escaping confusion with Python 3 + MySQL

Νίκος Βέργος me.on.nzt at gmail.com
Sun Mar 26 11:40:40 EDT 2017


Τη Κυριακή, 26 Μαρτίου 2017 - 6:28:12 μ.μ. UTC+3, ο χρήστης Chris Angelico έγραψε:
> On Mon, Mar 27, 2017 at 2:11 AM, Νίκος Βέργος <me.on.nzt at gmail.com> wrote:
> > Τη Κυριακή, 26 Μαρτίου 2017 - 5:49:00 μ.μ. UTC+3, ο χρήστης Ian έγραψε:
> >
> >> The database wrapper won't do substitution into the middle of a string
> >> like that. Either concatenate the literal %'s on in the SQL statement
> >> or add them to the string before you pass it in, i.e. '%' + domain +
> >> '%' or '%%%s%%' % domain or '%{}%'.format(domain).
> >
> > I just tried:
> >
> > domain = '.'.join( host.split('.')[-2:] )
> > domain = '%' + domain + '%'
> >
> > cur.execute('''UPDATE visitors SET (pagesID, host, ref, location, useros, browser, visits) VALUES (%s, %s, %s, %s, %s, %s, %s) WHERE host LIKE "%s" ''',
> >                                                                                                                                                                                                                                                                                         (pID, domain, ref, location, useros, browser, lastvisit, domain) )
> >
> >
> > and i received no error in the error_log but
> > ProgrammingError(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(pagesID, host, ref, location, useros, browser, visits) VALUES (1, '%cyta.gr%', ' at line 1")
> >
> > which you can see at http://superhost.gr
> >
> > You said somethign about concatenating the literal % in the SQL to which i didnt actually i understand how to implement.
> 
> Stop panicking.
> 
> Stop just trying one thing very quickly, getting an error message, and
> firing a post back to this list.
> 
> Slow down and read the messages you are getting.
> 
> You have already been told about UPDATE queries not using this syntax,
> and the error message is referring to that. You have an error in your
> SQL syntax; **check the manual**.

> This is the same problem that you had the last time you were here on
> this list - you had a different email address then, and I can't
> remember what name you were going by, but I won't quickly forget
> superhost.gr, which at the moment looks like Geocities found a new
> domain name.
> 
> ChrisA

I'am reading all the answers and trying everything mentioned.

As for the SQL syntax i dont see where the error is.
I use many UPDATE statements within my scripts in exactyly the same why i have been showing them to you.
The only UPDATE is failing is this one with the LIKE clause.

I think that Ian's is correct saying that the substitution is ain't done as we intend form the database wrapper which is PyMYSQL.

And last but not least it would be nice if you actually made a contributing comment regarding to my questions(which bothers me 3 days in a row by now) instead of making ironic remarks related to how my website looks like aesthitically to you.



More information about the Python-list mailing list