SQL查询生成未定义的charmap

问题描述 投票:1回答:1

这是我正在运行的代码,是firebird数据库中的SELECT命令。

我希望将某个表的内容写入.txt文件:

import fdb
con = fdb.connect(dsn=defaultDSN, user=defaultUser, password=defaultPassword)
cur = con.cursor()

cur.execute("SELECT * FROM TableName")

#I'm aware this erases everything in the file, this is intended
file = open(file="FirebirdMirror.txt", mode="w+", encoding='utf-8', errors='ignore')
file.write('')

file = open(file="FirebirdMirror.txt", mode="a+", encoding='utf-8', errors='ignore')

for fieldDesc in cur.description:
    file.write(fieldDesc[fdb.DESCRIPTION_NAME] + ', ')

file.write("\n")

for x in list(list(str(cur.fetchall()))):
    for y in x:
        file.write(str(y) + ', ')
    file.write('\n')


file.close()

我不知道为什么,但是我的cur.fetchall()返回一些异物...

Traceback (most recent call last):
  File "C:/Users/graciele.davince/PycharmProjects/helloworld/venv/firebirdSQL.py", line 205, in <module>
    generate_text_file()
  File "C:/Users/graciele.davince/PycharmProjects/helloworld/venv/firebirdSQL.py", line 166, in generate_text_file
    for x in list(list(str(cur.fetchall()))):
  File "C:\Users\graciele.davince\PycharmProjects\helloworld\venv\lib\site-packages\fdb\fbcore.py", line 3807, in fetchall
    return [row for row in self]
  File "C:\Users\graciele.davince\PycharmProjects\helloworld\venv\lib\site-packages\fdb\fbcore.py", line 3807, in <listcomp>
    return [row for row in self]
  File "C:\Users\graciele.davince\PycharmProjects\helloworld\venv\lib\site-packages\fdb\fbcore.py", line 3542, in next
    row = self.fetchone()
  File "C:\Users\graciele.davince\PycharmProjects\helloworld\venv\lib\site-packages\fdb\fbcore.py", line 3759, in fetchone
    return self._ps._fetchone()
  File "C:\Users\graciele.davince\PycharmProjects\helloworld\venv\lib\site-packages\fdb\fbcore.py", line 3412, in _fetchone
    return self.__xsqlda2tuple(self._out_sqlda)
  File "C:\Users\graciele.davince\PycharmProjects\helloworld\venv\lib\site-packages\fdb\fbcore.py", line 2843, in __xsqlda2tuple
    value = b2u(value, self.__python_charset)
  File "C:\Users\graciele.davince\PycharmProjects\helloworld\venv\lib\site-packages\fdb\fbcore.py", line 486, in b2u
    return st.decode(charset)
  File "C:\Users\graciele.davince\AppData\Local\Programs\Python\Python38-32\lib\encodings\cp1252.py", line 15, in decode
    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 3234: character maps to <undefined>

我的数据库可能包含巴西葡萄牙语的某些字符,即:

ç,â,ê,ô,ã,õ,á,é,í,ó,ú,à及其大写的表亲。

根据我的调查,这与文本如何以位形式存储有关,而0x9d位表示的字符似乎是问题。

我正在使用errors ='ignore',但错误仍然显示,使用encoding ='utf-8',并且我也尝试了latin-1,ISOnumbersnumbers,windows1252等,但无济于事。

  • 约束:我不能使用任何更改表内容的命令,并且必须将其中的所有内容存储在.txt文件中。

ps .:同一行中的列必须用逗号分隔,并且每一行都必须用\ n

python sql python-3.x firebird codepages
1个回答
1
投票

与Firebird通信时发生错误,而不是从文本文件中读取时发生错误,因此errors="ignore"指令不适用。

[您可能需要显式指定连接字符集(例如UTF8),因此FDB不使用连接字符集NONE,它会应用默认编码(在您的情况下为Cp1252)。

© www.soinside.com 2019 - 2024. All rights reserved.