【问题标题】:Chinese character doesn't support for Sqlite3 through CGI script汉字不支持Sqlite3 通过CGI 脚本
【发布时间】:2012-08-16 03:45:38
【问题描述】:

通过 Cgi 脚本将汉字插入 Sqlite3 对我不起作用。我可以使用查询浏览器工具从同一数据库中插入和选择汉字,但是当我为此使用 python 脚本时,它会显示错误。这是我用于创建数据库的查询

CREATE TABLE registrations ( m_username VARCHAR PRIMARY KEY COLLATE 'BINARY', m_identity VARCHAR, m_updatetime DATETIME );

然后这是我用于更新和从数据库中选择值的 cgi 脚本

#! /Python26/python
dbFile = 'D:/sqlite/registrations'
import cgi
import sqlite3
import xml.sax.saxutils

 query = cgi.parse()
db = sqlite3.connect(dbFile)

user = query.get('username', [None])[0]
identity = query.get('identity', [None])[0]
friends = query.get('friends', [])
print 'Content-type: text/plain\n\n<?xml version="1.0" encoding="utf-8"?>\n'

print "<result>"

if user:
try:
    c = db.cursor()
    c.execute("insert or replace into registrations values (?, ?,      datetime('now'))", (user, identity))
    print "\t<update>true</update>"
except:
    print '\t<update>false</update>'

for f in friends:
print "\t<friend>\n\t\t<user>%s</user>" % (xml.sax.saxutils.escape(f), )
c = db.cursor()
c.execute("select m_username, m_identity from registrations where m_username = ? and m_updatetime > datetime('now', '-1 hour')", (f, ))
for result in c.fetchall():
    eachIdent = result[1]
    if not eachIdent:
        eachIdent = ""
    print "\t\t<identity>%s</identity>" % (xml.sax.saxutils.escape(eachIdent), )
    if f != result[0]:
        print "\t\t<registered>%s</registered>" % (xml.sax.saxutils.escape(result[0]), )
print "\t</friend>"
db.commit()

print "</result>"

我想,我需要将 CHARSET 设置为 UTF-8,但我不知道该怎么做。我被谷歌搜索了,但找不到解决这个问题的好方法。请有人帮助我。

【问题讨论】:

  • "显示错误。"惊人的。喜欢分享吗?
  • 不是真的关于这个问题,但请先生,使用一个框架。

标签: python sqlite cgi


【解决方案1】:

我是通过客户端完成的。我只是使用 EncodeBase64 并对中文数据进行编码并发送到数据库。我认为,这不是直截了当的方法。但我找不到其他方法。

【讨论】:

    猜你喜欢
    • 2012-02-16
    • 2012-06-08
    • 2011-02-04
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多