我的代码似乎运行良好,没有任何错误,但它只是创建了一个空的数据库文件,其中没有任何内容,无法弄清楚我在这里做错了什么。
import sqlite3 as lite
import sys
con = lite.connect('test43.db')
def create_db():
with con:
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS Contacts")
cur.execute("CREATE TABLE Contacts (First Name TEXT, Last Name TEXT, Phone TEXT, Email TEXT);")
cur.execute("INSERT INTO Contacts VALUES (?, ?, ?, ?);", (firstname, lastname, phone, email))
cur.commit()
#Get user input
print ('Enter a new contact')
print ('')
firstname = input('Enter first name: ')
lastname = input('Enter last name: ')
phone = input('Enter phone number: ')
email = input('Enter Email address: ')
createnewdb = input('Enter 1 to create new db: ')
if createnewdb == 1:
create_db()
else:
sys.exit(0)