I can not enter user-defined values in SQLite

3

I have a problem where I can not add anything to the table and the error always appears:

  

sqlite3.InterfaceError: Error binding parameter 0 - probably   unsupported type.

The code I'm trying to add values to and this:

  p_name=input("Digite o nome do produto: "),
  p_desc=input("Digite a descrição: "),
  p_costp=input("Digite o preço de custo: "),
  p_sellp=input("Digite o preço de venda: ")
  c.execute("INSERT INTO products (name,description,costp,sellp) VALUES
(?,?,?,?)",(p_name,p_desc,p_costp,p_sellp))

And my Table:

c.execute("CREATE TABLE IF NOT EXISTS products(
    id INTEGER PRIMARY KEY AUTOINCREMENT, 
    name TEXT, 
    description TEXT, 
    costp REAL, 
    sellp REAL)")

Inserting with pre-defined values does work, but values I received from the user I can not and this error always appears.

    
asked by anonymous 30.01.2016 / 14:58

2 answers

3

First, using REAL to store monetary values is an error .

If you want to insist, you need to convert the data received by input() into a floating-point type to match the type REAL , like this:

c.execute("INSERT INTO products (name, description, costp, sellp)
              VALUES (?, ?, ?, ?)", (p_name, p_desc, float(p_costp), float(p_sellp)))

This is a simplistic form, but works if the value is typed correctly. Ideally the conversion would be done sooner and would be the exception if it did not work out. But as it seems to be just a test, this is not so important.

    
30.01.2016 / 15:13
0

A way to be passing the values to your very useful sql that always works, is to use the passing of parameters by tuples.

c.execute("INSERT INTO products (name,description,costp,sellp) VALUES ('%s','%s',%f,%f)" %(p_name,p_desc,p_costp,p_sellp))

Preferably, do not forget the single quotation marks for values of type TEXT

    
14.02.2016 / 22:04