Python从文本文件中读取查询(sqlite3.OperationalError)

时间:2014-06-30 13:47:45

标签: python sqlite

我想从文本文件中读取查询。但是有错误

    sqlite3.OperationalError: near "DELETE": syntax error

我的代码是:

    import codecs
    import sqlite3

    conn = sqlite3.connect('D:\\db.sqlite')
    cursor = conn.cursor()

    def main():   
        with codecs.open('D:\\delete.txt','r',encoding='utf8') as myfile:
            cursor.executescript(myfile.read())
            conn.commit()
        conn.close()

    if __name__ == '__main__' : main()  

我的文字文件是:

    DELETE FROM table1 WHERE  id = 1;
    DELETE FROM table1 WHERE  id = 2;

有什么问题?请帮帮我

1 个答案:

答案 0 :(得分:1)

查看文件的前3个字符......您可能会在(0xEF, 0xBB, 0xBF)找到UTF8字节顺序标记(BOM)。

要删除BOM,请使用'utf-8-sig'编码打开文件:

codecs.open('D:\\delete.txt', 'r', encoding='utf-8-sig')