<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Disbeliever</title>
  <subtitle>I was uncool before uncool was cool.</subtitle>
  <link rel="alternate" type="text/html" href="http://tewson.com/content/python-qt-mysql-%E0%B9%81%E0%B8%A5%E0%B8%B0%E0%B8%A0%E0%B8%B2%E0%B8%A9%E0%B8%B2%E0%B9%84%E0%B8%97%E0%B8%A2"/>
  <link rel="self" type="application/atom+xml" href="http://tewson.com/node/82/atom/feed"/>
  <id>http://tewson.com/node/82/atom/feed</id>
  <updated>2009-03-30T17:08:57+07:00</updated>
  <entry>
    <title>Python Qt MySQL และภาษาไทย</title>
    <link rel="alternate" type="text/html" href="http://tewson.com/content/python-qt-mysql-%E0%B9%81%E0%B8%A5%E0%B8%B0%E0%B8%A0%E0%B8%B2%E0%B8%A9%E0%B8%B2%E0%B9%84%E0%B8%97%E0%B8%A2" />
    <id>http://tewson.com/content/python-qt-mysql-%E0%B9%81%E0%B8%A5%E0%B8%B0%E0%B8%A0%E0%B8%B2%E0%B8%A9%E0%B8%B2%E0%B9%84%E0%B8%97%E0%B8%A2</id>
    <published>2009-03-30T17:08:56+07:00</published>
    <updated>2009-03-30T17:08:57+07:00</updated>
    <author>
      <name>tewson</name>
    </author>
    <category term="intellect" />
    <category term="programming" />
    <category term="python" />
    <summary type="html"><![CDATA[<p>วันนี้เขียนโปรแกรมโดยใช้ PyQt แล้วติดปัญหาว่าใส่ข้อมูลภาษาไทยลงฐานข้อมูลไม่ได้ โดยฐานข้อมูลใช้ charset เป็น UTF-8<br />
วิธีก็คือ ตอนเชื่อมต่อฐานข้อมูลให้ระบุไปด้วยว่าจะเชื่อมต่อแบบไหน<br />
<code><br />
conn = MySQLdb.connect (<br />
  host = "localhost",<br />
  user = "user",<br />
  passwd = "pass",<br />
  db = "db",<br />
  use_unicode=True,<br />
  charset='utf8')<br />
</code><br />
ต่อมา เนื่องจากเขียนอยู่บน PyQt พอจะเอาข้อความไปใส่ฐานข้อมูล ก็ต้องแปลงจาก QString เป็น String ธรรมดาก่อน ซึ่งจะใช้ฟังก์ชั่น str() เฉย ๆ ไม่ได้ เพราะมันเป็นภาษาไทย เลยต้องใช้ unicode() แทน<br />
ขอขอบคุณ <a href="http://www.narisa.com/forums/index.php?showtopic=15869" title="http://www.narisa.com/forums/index.php?showtopic=15869">http://www.narisa.com/forums/index.php?showtopic=15869</a></p>
    ]]></summary>
    <content type="html"><![CDATA[<p>วันนี้เขียนโปรแกรมโดยใช้ PyQt แล้วติดปัญหาว่าใส่ข้อมูลภาษาไทยลงฐานข้อมูลไม่ได้ โดยฐานข้อมูลใช้ charset เป็น UTF-8</p>
<p>วิธีก็คือ ตอนเชื่อมต่อฐานข้อมูลให้ระบุไปด้วยว่าจะเชื่อมต่อแบบไหน</p>
<p><code><br />
conn = MySQLdb.connect (<br />
  host = "localhost",<br />
  user = "user",<br />
  passwd = "pass",<br />
  db = "db",<br />
  use_unicode=True,<br />
  charset='utf8')<br />
</code></p>
<p>ต่อมา เนื่องจากเขียนอยู่บน PyQt พอจะเอาข้อความไปใส่ฐานข้อมูล ก็ต้องแปลงจาก QString เป็น String ธรรมดาก่อน ซึ่งจะใช้ฟังก์ชั่น str() เฉย ๆ ไม่ได้ เพราะมันเป็นภาษาไทย เลยต้องใช้ unicode() แทน</p>
<p>ขอขอบคุณ <a href="http://www.narisa.com/forums/index.php?showtopic=15869" title="http://www.narisa.com/forums/index.php?showtopic=15869">http://www.narisa.com/forums/index.php?showtopic=15869</a></p>
    ]]></content>
  </entry>
</feed>
