pwwang / pymedoo

A lightweight database framework for python
MIT License
15 stars 4 forks source link

Error if None in value: "sqlite3.OperationalError: no such column: None" #4

Closed void285 closed 5 years ago

void285 commented 5 years ago

Hi, I encountered error in this code:

#coding: utf-8
from medoo import Medoo

db = Medoo(dbtype="sqlite", database="test.db")
db.query('''create table table_1
    (id integer primary key,
    name varchar not null default "",
    cate varchar not null default "",
    ctime int not null default 0
    );''')
data = [
    {'name': None, 'cate': 'Alysia', 'ctime': 1},
    {'name': 'Gwenda', 'cate': 'Chang', 'ctime': 2},
    {'name': 'Fannie', 'cate': 'Jonelle', 'ctime': 3},
    {'name': 'Meggan', 'cate': 'Oliver', 'ctime': 4},
    {'name': 'Rueben', 'cate': 'Estefana', 'ctime': 5},
]
db.insert('table_1', *data)
Traceback (most recent call last):
  File "test2.py", line 18, in <module>
    db.insert('table_1', *data)
  File "C:\Develop\Python27\lib\site-packages\medoo\base.py", line 70, in insert
    return self.query(sql, kwargs.get('commit', True))
  File "C:\Develop\Python27\lib\site-packages\medoo\base.py", line 116, in query
    raise type(ex)(str(ex) + ':\n' + self.sql)
sqlite3.OperationalError: no such column: None:
INSERT INTO "table_1" ("ctime","name","cate") VALUES (1,None,'Alysia'),(2,'Gwenda','Chang'),(3,'Fannie','Jonelle'),(4,'Meggan','Oliver'),(5,'Rueben','Estefana')

And, I once met with this error in a long and complicated insert, the inserted values have None in it, after replacement of None with empty string, the error disappeared. I have no idea about what may have caused it.

  File "C:\Develop\Python27\lib\site-packages\medoo\base.py", line 113, in query
    raise type(ex)(str(ex) + ':\n' + self.sql)
sqlite3.OperationalError: <exception str() failed>
void285 commented 5 years ago

Same error if True/False for boolean field in inserted values.

pwwang commented 5 years ago

This is because that there is no boolean values and None for sqlite. I will try to map True to 1, False to 0 and None to NULL.

pwwang commented 5 years ago

I fixed it at 0.0.4. However, with your original code, None would be casted into NULL, which will raise an exception since you defined column name as not null.