如何在MySQL中允许整数使用空字符串? [英] How to allow empty string for integer in MySQL?

查看:62
本文介绍了如何在MySQL中允许整数使用空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表中有整数字段. POST是通过复杂的JavaScript发送的.它们发送像"这样的空字符串,但是正如您猜测的那样,MySQL不允许在整数字段中使用空字符串.有没有允许空字符串的选项?就像它需要一个空字符串一样,它将另存为NULL.

I have integer fields in a table. The POSTs are send by a complicated JavaScript. They send empty string like "" but as you guess MySQL doesn't allow emty strings in integer fields. Is there any option to allow empty strings? Like if it takes empty string it will save it as NULL.

推荐答案

从my.ini中删除sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"已解决了该问题.

Removing sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" from my.ini has solved the issue.

删除上面的行是可行的,但这不是一个好主意.它允许有诸如0000-00-00或空字符串日期之类的内容.最好保持上面的行,不要将空字符串插入整数字段,而是将空字符串转换为NULL,然后将该NULL插入整数字段.

Removing the line above works but it is a bad idea. It allows to have things like 0000-00-00 or empty string dates. Better keep the line above and don't insert empty sting into an integer field, instead convert empty string into NULL and then insert that NULL into integer field.

这篇关于如何在MySQL中允许整数使用空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆