Python Qt MySQL และภาษาไทย

วันนี้เขียนโปรแกรมโดยใช้ PyQt แล้วติดปัญหาว่าใส่ข้อมูลภาษาไทยลงฐานข้อมูลไม่ได้ โดยฐานข้อมูลใช้ charset เป็น UTF-8

วิธีก็คือ ตอนเชื่อมต่อฐานข้อมูลให้ระบุไปด้วยว่าจะเชื่อมต่อแบบไหน


conn = MySQLdb.connect (
host = "localhost",
user = "user",
passwd = "pass",
db = "db",
use_unicode=True,
charset='utf8')

ต่อมา เนื่องจากเขียนอยู่บน PyQt พอจะเอาข้อความไปใส่ฐานข้อมูล ก็ต้องแปลงจาก QString เป็น String ธรรมดาก่อน ซึ่งจะใช้ฟังก์ชั่น str() เฉย ๆ ไม่ได้ เพราะมันเป็นภาษาไทย เลยต้องใช้ unicode() แทน

ขอขอบคุณ http://www.narisa.com/forums/index.php?showtopic=15869

No comments available.

Add your comment

The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Links

Recent comments