You may want to try it this way:

result_article_db = db.query("select * from articles where
articles.titre = $title",vars=dict(title=name))

or better:

result_article_db = db.select("articles", dict(title=name),
where="titre = $title")



On Sep 19, 4:39 am, boubou_cs <[email protected]> wrote:
> Hi, all !
>
> I'm having a problem I can not deal with the French characters.
> It seems there is a problem with db.py on line 124 :
>
>         /usr/lib/python2.5/site-packages/web/db.py", line 124, in __init__
> self.items = [str(items)]
>         UnicodeEncodeError: 'ascii' codec can't encode characters in position
> 56-57: ordinal not in range(128)
>
> I tried some solutions but without success, including that of Max I
> found here :
>        http://www.mail-archive.com/[email protected]/msg04618.html
>
> I post here a little piece of my code so you can keep track of my
> research.
>
> I hope you'll help me, because I'm completely stuck on it :/
>
> First, the simple mysql table 'charsetproblem.mysql':
>
> #-----------------------------------------------------------#
> # Usage:        mysql -u mylogin -p mybase < charsetproblem.mysql #
> #               Open  : mysql -u mylogin -p mybase                      #
> #                       mysql>          SHOW TABLES;                         #
> #                       mysql>          select * from articles;              #
> #                       mysql>          DROP TABLE IF EXISTS articles;       #
> #                                                                             
>                   #
>
> # --- create table ---#
>
> CREATE TABLE articles (
> id int(10) NOT NULL auto_increment,
> titre char(80)  DEFAULT "" NOT NULL,
> article text(4096)  DEFAULT "" NOT NULL,
> PRIMARY KEY (id)
> )DEFAULT CHARACTER SET utf8;
>
> # Note: i tried with and without DEFAULT CHARACTER SET utf8;#
>
> # --- insert datas ---#
>
> INSERT INTO articles VALUES (NULL, "simple link without french
> characters", "Lorem ipsum dolor sit amet, consectetur adipisicing
> elit, sed do eiusmod tempor incididunt ut labore et dolore magna
> aliqua.");
>
> INSERT INTO articles VALUES (NULL, "simple link with frènch
> chàracters", "Lorém ipsum dôlor sit amet, çonsèctetur àdipisicing
> elit, sed do eiusmod tempor incididunt ut labore et dolore magna
> aliqua.");
> #-----------------------------------------------------------#
>
> Then, the piece of script 'code.py':
>
> #-----------------------------------------------------------#
> #!/usr/bin/env python
> #-*- coding: iso-8859-1 -*-
>
> # Note:         See charsetproblem.mysql to install table 'article'.
> #               Change your settings for user, pw and db (web.database 
> lowest).
> #               Create a directory named 'templates" in the same directory as 
> this
> #               script.
> #
>
> import web
> from web import form
>
> web.config.debug = False
>
> db = web.database(dbn='mysql', user='myname', pw='mypassword',
> db='mydb')
>
> render = web.template.render('templates/')
>
> urls = (
>         '/(.*)', 'index'
>         )
>
> app = web.application(urls, globals())
>
> class index:
>         """
>         """
>         def __init__(self):
>                 """  """
>
>         def GET(self, name = "index"):
>                 """ shows the home page """
>
>                 # I try with those charsets without success
>                 web.header("Content-Type", "text/html; charset= utf-8")
>                 #web.header("Content-Type", "text/html; charset= iso-8859-1")
>
>                 links = []
>                 article =[]
>
>                 # --- LINKS -
>
>                 # All titles become links
>                 result_titre_db = db.query('select titre from articles;')
>                 for i in result_titre_db:
>                         links.append(i.titre)
>                         # Curiously, if I try this one, link's charset is 
> correct...
>                         #links.append(i.titre.encode('raw_unicode_escape'))
>
>                 # --- ARTICLES -
>
>                 # It is the error which shows the 'self.items' error in db.py,
>                 # even if I change charset in web.header above:
>                 # File "/usr/lib/python2.5/site-packages/web/db.py", line 
> 124, in
> __init__
>                 # self.items = [str(items)]
>                 # UnicodeEncodeError: 'ascii' codec can't encode characters in
> position 56-57: ordinal not in range(128)
> #               query = 'select article from articles where articles.titre = 
> "%s";'
> % name
>
>                 # Other tests:
>                 # No error, but bad charset even if I change charset in 
> web.header
> above.
>                 query = 'select article from articles where articles.titre = 
> "%s";'
> % name.encode("utf-8")
> #               query = 'select article from articles where articles.titre = 
> "%s";'
> % name.encode('raw_unicode_escape')
> #               query = 'select article from articles where articles.titre = 
> "%s";'
> % name.encode('iso-8859-15')
>
>                 result_article_db = db.query(query)
>                 for i in result_article_db:
>                         article.append(i.article)
>                 if article == []:
>                         return web.notfound()
>
>                 return render.index(name, links, article)
>
> if __name__ == "__main__":
>         app.run()
>
> # I tried the solution given by 
> Maxhttp://www.mail-archive.com/[email protected]/msg04618.html
> # without success.
> #-----------------------------------------------------------#
>
> I have all parameterized and tested to facilitate your testing if you
> are ready to help.
>
> Thank you, I really do not know how to solve this problem
>
> Regards,
>
> boubou.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to