error to use cursor to save escaped characters

50 views
Skip to first unread message

fanchyna

unread,
Jul 25, 2012, 5:40:04 PM7/25/12
to django...@googlegroups.com
I have a url which I want to save into the MySQL database using the "cursor" tool offered by django, but I keep getting the "not enough arguments for format string" error because this url contains some escaped characters (non-ascii characters). The testing code is fairly short:

#test.py#
  
import os
import runconfig #configuration file
os.environ['DJANGO_SETTINGS_MODULE'] = runconfig.django_settings_module
from django.db import connection,transaction
c = connection.cursor()
url = "http://www.academicjournals.org/ijps/PDF/pdf2011/18mar/G%C3%B3mez-Berb%C3%ADs et al.pdf"

dbquery = "INSERT INTO main_crawl_document SET url="+url
c.execute(dbquery)
transaction.commit_unless_managed()

The full error message is

Traceback (most recent call last):
  File "./test.py", line 14, in <module>
    c.execute(dbquery)
  File "/usr/local/lib/python2.6/site-packages/django/db/backends/util.py", line 38, in execute
    sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  File "/usr/local/lib/python2.6/site-packages/django/db/backends/__init__.py", line 505, in last_executed_query
    return smart_unicode(sql) % u_params
TypeError: not enough arguments for format string

Can anybody help me? 

Message has been deleted

fanchyna

unread,
Jul 30, 2012, 11:42:28 AM7/30/12
to django...@googlegroups.com, wlf...@ix.netcom.com
Good answer. Using the c.execute(command,parameter) solves my problem. 

On Wednesday, July 25, 2012 8:49:07 PM UTC-4, Dennis Lee Bieber wrote:
On Wed, 25 Jul 2012 14:40:04 -0700 (PDT), fanchyna <fanc...@gmail.com>
declaimed the following in gmane.comp.python.django.user:

>
> > import os
> > import runconfig #configuration file
> > os.environ['DJANGO_SETTINGS_MODULE'] = runconfig.django_settings_module
> > from django.db import connection,transaction
> > c = connection.cursor()
> > url =
> > "http://www.academicjournals.org/ijps/PDF/pdf2011/18mar/G%C3%B3mez-Berb%C3%ADs
> > et al.pdf"
> >
> > dbquery = "INSERT INTO main_crawl_document SET url="+url
> > c.execute(dbquery)
> > transaction.commit_unless_managed()
>
        ONE: That is not a valid SQL statement for INSERT or UPDATE ...

        INSERT INTO table (fieldlist) VALUES (valuelist)
        UPDATE table SET field = value WHERE key=identifier

        TWO: NEVER build up your query by hand, USE the DB-API parameter
system to safely quote parameters...

        dbquery = "insert into main_crawl_document (url-or-whatever-field)
values (%s)"
        c.execute(dbquery, url)

{note: MySQLdb uses %s for the placeholder, SQLite3 uses ? for
placeholder, other RDBMs could use other syntax -- removing these
concerns is one goal of using RDBM-agnostic ORM systems}
--
        Wulfraed                 Dennis Lee Bieber         AF6VN
        wlf...@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

fanchyna

unread,
Jul 30, 2012, 11:44:47 AM7/30/12
to django...@googlegroups.com, wlf...@ix.netcom.com
but just want to let you know that my INSERT INTO statement was correct. you can google "insert mysql" to check. the "INSERT into" support three forms. 


On Wednesday, July 25, 2012 8:49:07 PM UTC-4, Dennis Lee Bieber wrote:
On Wed, 25 Jul 2012 14:40:04 -0700 (PDT), fanchyna <fanc...@gmail.com>
declaimed the following in gmane.comp.python.django.user:

>
> > import os
> > import runconfig #configuration file
> > os.environ['DJANGO_SETTINGS_MODULE'] = runconfig.django_settings_module
> > from django.db import connection,transaction
> > c = connection.cursor()
> > url =
> > "http://www.academicjournals.org/ijps/PDF/pdf2011/18mar/G%C3%B3mez-Berb%C3%ADs
> > et al.pdf"
> >
> > dbquery = "INSERT INTO main_crawl_document SET url="+url
> > c.execute(dbquery)
> > transaction.commit_unless_managed()
>
Reply all
Reply to author
Forward
Message has been deleted
0 new messages