当前位置:Gxlcms > 数据库问题 > insert NULL into mysql

insert NULL into mysql

时间:2021-07-01 10:21:17 帮助过:20人阅读

Note that because you have to handle None differently, you also have to handle proper SQL quoting! If any of your values directly or indirectly come from user-supplied data, you‘d be open for SQL injection attacks otherwise.

The above sqlquote() function should suffice for SQLite strings, which follow standard SQL quoting rules. Different databases have different rules for this, so tripple check your documentation.

Personally, I‘d use the SQLAlchemy library to generate SQL and have it handle quoting for you. You can configure it to produce SQL for different database engines.

 

插入空的datetime类型:

sql = "INSERT INTO test_data_table (`data`, char_test, datetime_test) VALUES (%s, %s, %s)" % (‘NULL‘, ‘NULL‘, "‘2017-09-01‘")
sql = "INSERT INTO test_data_table (`data`, char_test, datetime_test) VALUES (%s, %s, %s)" % (‘NULL‘, ‘NULL‘, ‘NULL‘)
注意两者之间的不同。

简而言之,python遇到None,需要在数据库插入‘NULL‘需要有一个转化过程,将None转为NULL,并视情况加单双引号,不加‘‘在%s.

 

insert NULL into mysql

标签:values   person   ret   into   code   time   base   efault   ref   

人气教程排行