我正在使用Python创建一个注册系统,将值添加到SQLite数据库中。使用我的代码,运行时不会出现任何错误,但数据不会添加到表中。
import sqlite3 as db #Import the SQLite module as db
import smtplib as mail #Importing the send email library as mail
from email.mime.text import MIMEText #Importing the text component of the mail library
#Getting the user to input the their data
username = raw_input("What would you like your username to be? ")
email = raw_input("What is your email address? ")
firstName = raw_input("What is your first name? ")
surname = raw_input("What is your surname? ")
age = raw_input("How old are you? ")
password = raw_input("What is your password?") #Encryption method will be added later
int(age) #Changing the age variable into an integer so it can be inputted into the database
#DB part
conn = db.connect('apollo.db') #Connecting to the database
cursor = conn.cursor()
cursor.execute("INSERT INTO users VALUES(?, ?, ?, ?, ?, ?)",
(username, email, firstName, surname, age, password)) #Inserting the user's data into the table
conn.close() #Closing the connection
答案 0 :(得分:3)
您需要commit()
更改。
答案 1 :(得分:0)
就像pbacterio已经回答的那样,你必须通过调用cursor.commit()来提交你的更改,所以任何被缓冲的动作(在另一个文件中,如果我没记错的话)将被写入你的数据库文件中。
答案 2 :(得分:0)
也可以使用
age= int(age)
int(age)返回整数值,但您需要存储回来。