如何在数据库中输入值?

时间:2018-04-04 05:19:38

标签: python database python-3.x sqlite

import easygui as eg
import sqlite3``        

conn = sqlite3.connect('ATM.sqlite')
        print("Connected to database successfully")
        conn.execute("CREATE TABLE if not exists CUSTOMER(ID INTEGER, Name TEXT, Address TEXT, Mobile_no INTEGER, PIN INTEGER, Balance INTEGER)")

    def Admin_AddUser():
        options = ['ID', 'Name', 'Address', 'Mobile No.', 'PIN']
        Current_bal = 0
        value = eg.multenterbox('Enter the information for new user', 'Add User', options)
        conn.execute('INSERT INTO CUSTOMER (ID, Name, Address, Mobile_no, PIN, Balance) VALUES'
                     '( value[0],value[1],value[2],value[3],value[4],int(Current_bal))')

我希望在客户表中输入详细信息。但它向我显示以下错误。

    sqlite3.OperationalError: near "[0]": syntax error

2 个答案:

答案 0 :(得分:1)

data =  [value[0],value[1],value[2],value[3],value[4],int(Current_bal)]

cursor.execute('INSERT INTO CUSTOMER (ID, Name, Address, Mobile_no, PIN, Balance) VALUES ( ?,?,?,?,?,?)', data)

答案 1 :(得分:1)

使用参数:

conn.execute('INSERT INTO CUSTOMER (ID, Name, Address, Mobile_no, PIN, Balance) 
              VALUES (?,?,?,?,?,?)',
             [value[0],value[1],value[2],value[3],value[4],int(Current_bal)])