通过Python将数据插入SQLite数据库

时间:2017-11-22 15:56:10

标签: python sqlite

我没有收到任何错误,但是当我在SQLite中检查我的数据库时。它显示空集。我该怎么办?

我的班级:

初始化

def __init__ (self):

    self.name = ""
    self.age = 0
    self.weight = 0.0
    self.height = 0.0
    self.gender = ""
    self.email = ""
    self.password = "'

主要功能

def sign_up (self):


    self.name = raw_input ("Enter your first Name :")
    self.email = raw_input ("Enter A Valid Email id :")

    length = int(input ("How long do you want your password to be ? : "))
    chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'

    for c in range (length):
        self.password += random.choice(chars)
        print
        print "Your Special Password is :" , self.password

    print
    self.gender = raw_input ("enter your Gender (male / female) :")
    self.age = input ("Enter Your Age :")
    self.weight = input ("Enter Your Weight :")
    self.height = input ("Enter Your Height :")
    print
    print "Information Saved..."
    print

数据库部分从这里开始

    conn = sqlite3.connect('data.db')
    c = conn.cursor()



    params = (self.name, self.email, self.password, self.gender, self.age, 
              self.weight, self.height)

    c.execute("INSERT INTO users VALUES (?, ?, ?, ?, ?, ?, ?)", params)
    conn.commit()
    c.close()
    conn.close()

1 个答案:

答案 0 :(得分:1)

您正在创建一个用于插入数据的游标,这在sqlite3

中是不需要的
conn = sqlite3.connect('data.db')
params = (self.name, self.email, self.password, self.gender, self.age, 
          self.weight, self.height)

conn.execute("INSERT INTO users VALUES (?, ?, ?, ?, ?, ?, ?)", params)
conn.commit()
conn.close()