I know the problems with converting decimals to floats and vice versa. At the moment, I have a program that runs nearly always using the decimal
module, but when I try to add any decimal.Decimal
data type into my database it throws the error:
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
Here is the code:
db = sqlite3.connect('test.db')
c = db.cursor()
c.execute('CREATE TABLE IF NOT EXISTS test1 (decimal REAL)')
a = Decimal(2.14) - Decimal(1.55)
c.execute("INSERT INTO test1(decimal) VALUES(?)",(a,))
db.commit()
but if I change Decimal
to float
it will work.
will this affect the accuracy of the answers (I'm pretty sure it does)?
is there a better way of doing this than to convert to float in order to store in database? If I can't store it directly in
Decimal
type, than I will need to be able to convert it back toDecimal
after pulling it from my database.- Why doesn't the "DECIMAL" data type work for this? what am I missing here?