pudo / dataset

Easy-to-use data handling for SQL data stores with support for implicit table creation, bulk loading, and transactions.
https://dataset.readthedocs.org/
MIT License
4.76k stars 297 forks source link

Can't insert list in dictionaries #248

Closed DanielVip3 closed 6 years ago

DanielVip3 commented 6 years ago

I don't know if that's an issue or an impossible thing, but I'll briefly explain :
I wanted to do a "badge system". As always, I tried to make a dictionary with this library, but this time it didn't work, because I was using a list.

mottdb = dataset.connect('sqlite:///prova.db')

badge = mottdb['badge']

badge.insert(dict(IDuser=294897792919404544, badge=["badge", "badgetwo"]))
badge.insert(dict(IDuser=228923618246000642, badge=["badge", "badgetwo"]))

The error that shows up is :

in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.InterfaceError: (sqlite3.InterfaceError) Error binding parameter 1 - probably unsupported type. [SQL: 'INSERT INTO badge ("IDutente", badge) VALUES (?, ?)'] [parameters: (294897792919404544, ['badge', 'badgedue'])] (Background on this error at: http://sqlalche.me/e/rvf5)

I also tried with tuples and the list(["badge", "badgetwo"]) method, but nothing. Is there a way to make a list in the dictionary without problems?

pudo commented 6 years ago

What you're proposing is to store an array of values in a single SQL column? While some specific SQL engines, like PostgreSQL do this, it's not really a common feature. SQLite, which you seem to be using, doesn't have an ARRAY type. More generally, it looks to me like the thing you're trying to do is an actual app rather than a data munging script. Perhaps it might make sense to switch to using either SQLAlchemy with a joined table, or a database like MongoDB that actually has arrays in each document.