我在SQLite3 Python3上做错了什么

时间:2017-05-21 10:52:09

标签: python-3.x sqlite

我无法弄清问题是什么。我收到以下错误消息: sqlite3.OperationalError:没有这样的表:Atm。这发生在第34行,一旦到达cursor.execute(sql),就是insertData函数。一切似乎都运转良好,任何人都可以帮忙吗?

当我按“Y”在第14行重新创建当前表时,这会崩溃。

   import sqlite3
    import time

def create_table(dbName, table_name, sql):
    with sqlite3.connect('ATM.db') as db:
        cursor = db.cursor()
        cursor.execute("select name from sqlite_master where name=?",(table_name,))
        result = cursor.fetchall()
        keep_table = True
        if len(result) == 1:
            response = input("The table {0} already exists, do you wish to recreate it (y/n)".format(table_name))
            if response.lower() == "y":
                keep_table = False
                print("The {0} table will be recreated".format(table_name))
                cursor.execute("drop table if exists {0}".format(table_name))
                db.commit()
                insertData()
            else:
                print("The existing table was kept")
                insertData()
        else:
            keep_table = False
        if not keep_table:
            cursor.execute(sql)
            db.commit()
            insertData()


def insertData():
    with sqlite3.connect("ATM.db") as db:
        cursor = db.cursor()
        sql = """INSERT INTO Atm(Title, First, Surname, Balance) VALUES ('Mr.', 'Jeremy', 'Clarkson', 172.16),
                                                                        ('Miss', 'Suzanne', 'Perry', 15.62)"""
        cursor.execute(sql)
        db.commit()

db_name = "ATM.db"
sql = """ create table Atm
        (CustomerID integer,
        Title text,
        First text,
        Surname text,
        Balance real,
        primary key(CustomerID))"""
create_table(db_name, "Atm", sql)

sqlite3.OperationalError:没有这样的表:Atm

0 个答案:

没有答案