每当我尝试在我的dastabase中插入一个语句时,我得到一个“不正确的十进制值:第一行''bounty3''的'NULL'错误。如何将空值插入十进制数据类型?我应该将默认值设为0.00吗?
Incorrect decimal value: '' for column 'bounty3' at row 1 Whole query: INSERT INTO songs (userid, wavURL, mp3URL, genre, songTitle, BPM, insWanted, bounty, insWanted2, bounty2, insWanted3, bounty3, insWanted4, bounty4, insWanted5, bounty5, insWanted6, bounty6, insWanted7, bounty7, insWanted8, bounty8, insWanted9, bounty9, insWanted10, bounty10) VALUES ('12534545', '/audio/wav/jqmrgpfcMichael/135259578210secreason.wav', '/audio/mp3/jqmrgpfcMichael/135259578210secreason.mp3', 'Rock/Funk', 'titlee', '120', 'bass', '20.00', 'guitar', '20.00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '')
我也用NULL值尝试了这个语句。这是错误:
Incorrect decimal value: 'NULL' for column 'bounty3' at row 1 Whole query: INSERT INTO songs (userid, wavURL, mp3URL, genre, songTitle, BPM, insWanted, bounty, insWanted2, bounty2, insWanted3, bounty3, insWanted4, bounty4, insWanted5, bounty5, insWanted6, bounty6, insWanted7, bounty7, insWanted8, bounty8, insWanted9, bounty9, insWanted10, bounty10) VALUES ('12534545', '/audio/wav/jqmrgpfcMichael/143922765110secreason.wav', '/audio/mp3/jqmrgpfcMichael/143922765110secreason.mp3', 'Rock/Funk', 'title', '110', 'bass', '110.00', 'guitar', '20.00', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL')
答案 0 :(得分:13)
哦..你正试图在bounty3中插入一个空字符串''。将其替换为NULL。我还注意到其他可能的数值的空字符串,例如bonty4。您应该用NULL替换所有空字符串以获取数值。
例如:mysql_query(“INSERT INTO empty_number,number VALUES(NULL,1)”);
编辑:HEY HEY得到了这一点,你不能将NULL作为'NULL'插入数值,因为这是一个字符串,你应该插入NULL而不带任何引号
答案 1 :(得分:5)
如果您想要默认值,请使用空白,而不是空。
答案 2 :(得分:3)
我刚刚在MySql 5.1.61中处理过这个问题。 如果服务器处于严格模式,则必须为十进制字段输入0而不是''。禁用严格模式允许您只执行''填充空值。
答案 3 :(得分:1)
您可以运行此查询以允许'bounty3'字段中的NULL值。
ALTER TABLE songs
CHANGE bounty3
bounty3
DECIMAL(10,0)NULL;
答案 4 :(得分:0)
您需要确保数据库架构允许该列的NULL值。在MySQL客户端中运行describe <tablename>;
以查看该表的架构是什么。
如果架构不允许null,则可以使用alter table
更改架构。确保您没有为该列指定NOT NULL
,您应该没问题。
答案 5 :(得分:0)
确保字段类型允许NULL
值,默认值为NULL
。
然后使用:
UPDATE table_name SET date_field=IF('$date_value'='',NULL,'$date_value')
这适用于插入或更新。
我尝试使用$date_value = NULL
,我甚至在插入或更新之前尝试了unset($date_value)
,它从未在我的十进制字段上工作。 MySQL总是将它们转换为0.00。上面的方法是唯一对我有用的解决方案。
答案 6 :(得分:0)
谢谢,当它变为空时它使用NULL:
$conn->bindParam(':'.$valor, ( $key[0] ? $key[0] : NULL ), PDO::PARAM_STR);
答案 7 :(得分:0)
另一种选择是确保在源数据中,空单元格或字段包含“空白”字符。在MySQL中导入它们之前。这样,MySQL会将这些字段识别为真正 NULL,并且不会将它们转换为0。
答案 8 :(得分:0)
NOT NULL
)将字段的默认值设为NULL。
例:price decimal(12,2) DEFAULT NULL
答案 9 :(得分:0)
该字段很可能是数字。如果该字段为数字,则仅接受数字。不能接受空字符串
plugins {
// Apply the Kotlin JVM plugin to add support for Kotlin on the JVM.
id("org.jetbrains.kotlin.jvm").version("1.3.31")
// Apply the application plugin to add support for building a CLI application.
application
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
mavenCentral()
jcenter()
}
dependencies {
// Use the Kotlin JDK 8 standard library.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")
// Use the Kotlin JUnit integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
compile("io.dropwizard:dropwizard-core:1.3.14")
}
application {
// Define the main class for the application
mainClassName = "dropwizard.tut.AppKt"
}
tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = application.mainClassName
}
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
tasks.named<JavaExec>("run") {
args("server", "config/local.yaml")
}
都不是NULL字符串
insert into songs (...,bounty3) values(...,'')
(“ NULL”只是一个字符串,与实际的NULL值无关,因此,如果需要,“ NULL”类似于“ HONEY BEE”)。
因此,如果该列为数字,则它将仅包含数字。但是,如果列为NULLABLE,那么它也将接受NULL。这意味着您的插入内容必须具有
insert into songs (...,bounty3) values(...,'NULL')
如果在这样的列上按NULL并且该列具有默认值,则将使用DEFAULT值,而不是您要推送的NULL。
答案 10 :(得分:0)
当收到 POST 数据时它对我有用: ... if ($value == '') {$value = '0.0';} ...