未定义的名称'错误'在Python中

时间:2017-10-07 19:26:27

标签: python

我正在尝试使用Python3连接sqlite db。

代码 -

import sys,os
import datetime as dt
import sqlite3
from datetime import date,datetime

def create_connection(db_file):
    """ create a database connection to the SQLite database
        specified by db_file
    :param db_file: database file
    :return: Connection object or None
    """
    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        print(e)

    return None


def main():
  database = r"C:\Users\prith\Documents\sqlitedb\filedb.db"
# create a database connection
    conn = create_connection(database)

if __name__ == '__main__':
    main()

我收到错误 - there is an undefined name called 'Error'

非常感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:2)

看起来非常清楚,名称Error尚未定义。我相信您的意思是使用sqlite3.Error代替。