zoukankan      html  css  js  c++  java
  • mysql Column count doesn't match value count at row 1

    今天执行批量插入的操作,发现报了错 mysql Column count doesn't match value count at row 1。

    后来发现原因:是由于写的SQL语句里列的数目和后面的值的数目不一致, 比如insert into 表名 (field1,field2,field3) values('a','b')这样前面的是三列,后面却只有二个值,这就会出现这个错误的。 

    查看mapper写法

     1 <insert id="insertBatch" useGeneratedKeys="true" parameterType="java.util.List">
     2     INSERT INTO sys_basic_info(
     3     <include refid="dbColumns" />
     4     ) VALUES
     5     <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
     6         #{item.groupId},
     7         #{item.groupName},
     8         #{item.groupCode}
     9     </foreach>
    10 </insert>

    原因很容易发现,foreach标签属性open和close配置有误!这样大括号会把整体包起来,正确应为:

     1 <insert id="insertBatch" useGeneratedKeys="true" parameterType="java.util.List">
     2     INSERT INTO sys_basic_info(
     3     <include refid="dbColumns" />
     4     ) VALUES
     5     <foreach collection="list" item="item" index="index" separator=",">
     6         (
     7         #{item.groupId},
     8         #{item.groupName},
     9         #{item.groupCode}
    10         )
    11     </foreach>
    12 </insert>
  • 相关阅读:
    InnoDB in Mysql
    Store engine for Mysql
    Replication in Mysql
    Mysql note 3
    查看SQL对象的创建脚本
    Mysql note 2
    Jsp登录后数据采集奇怪的Apache服务器
    一行代码收集页
    使用Subsonic与ObjectDataSource(ODS)
    二分查找
  • 原文地址:https://www.cnblogs.com/guanghe/p/10442318.html
Copyright © 2011-2022 走看看