1 回答
TA贡献1804条经验 获得超3个赞
您可以创建数据库并在之后更改编码
>>> import sqlite3
>>> conn = sqlite3.connect('example.db')
>>> c = conn.cursor()
>>> c.execute('pragma encoding')
<sqlite3.Cursor object at 0x7fa641241e30>
>>> rows = c.fetchall()
>>> for row in rows:
... print(row)
...
('UTF-8',)
>>> c.execute('pragma encoding=UTF16')
<sqlite3.Cursor object at 0x7fa641241b20>
>>> c.execute('pragma encoding')
<sqlite3.Cursor object at 0x7fa641241e30>
>>> rows = c.fetchall()
>>> for row in rows:
... print(row)
...
('UTF-16le',)
请注意,需要编辑数据库才能使这些更改永久化,例如:
>>> sql_create_projects_table = """ CREATE TABLE IF NOT EXISTS projects (
... id integer PRIMARY KEY,
... name text NOT NULL,
... begin_date text,
... end_date text
... ); """
>>> c.execute(sql_create_projects_table)
<sqlite3.Cursor object at 0x7f441ce90e30>
>>> rows = c.fetchall()
>>> for row in rows:
... print(row)
...
>>> sql = ''' INSERT INTO projects(name,begin_date,end_date)
... VALUES(?,?,?) '''
>>> project = ('Cool App with SQLite & Python', '2015-01-01', '2015-01-30');
>>> c.execute(sql, project)
<sqlite3.Cursor object at 0x7f441ce90e30>
如果您至少不添加表,编码将回退到其默认值。希望这可以帮助。
添加回答
举报