免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5547 | 回复: 7
打印 上一主题 下一主题

求助 搜索问题 Mysql::Error: Column count doesn't match value count at row 1:  关闭 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-23 08:10 |只看该作者 |倒序浏览
用rails搭建了一个网站,其他的都好着,就是搜索的时候会出错。控制台错误提示如下:

SQL (0.000293)   DROP TABLE IF EXISTS search_results
  SQL (0.000524)   CREATE TEMPORARY TABLE search_results (id INT AUTO_INCREMENT UNIQUE KEY, result_type VARCHAR(255), result_id INT)
  SQL (0.000000)   Mysql::Error: Column count doesn't match value count at row 1: INSERT INTO search_results VALUES (NULL, '0.09232217')


提示插入的数据与表结构不匹配,这个是相关的sql语句,返回的是三列,可不知到怎么回事,最后就少了一列。
我实在看不出哪有问题,请大家帮忙看看,谢谢了!
def create_search_results_table(search_query, models)

      solr_results = models.first.multi_solr_search(search_query,
          :models         => models,
          :results_format => :ids,
          :limit          => Conf.max_search_size)

      conn =  ActiveRecord::Base.connection

      conn.execute("CREATE TEMPORARY TABLE search_results (id INT AUTO_INCREMENT UNIQUE KEY, result_type VARCHAR(255), result_id INT)")

      # This next part converts the search results to SQL values
      #
      # from:  { "id" => "Workflow:4" }, { "id" => "Pack:6" }, ...
      # to:    "(NULL, 'Workflow', '4'), (NULL, 'Pack', '6'), ..."
   #下面是将查询的结果转化成 (NULL, 'Pack', '6')的形式,返回的是三个值
      if solr_results.results.length > 0
        insert_part = solr_results.results.map do |result|
          "(NULL, " + result["id"].split(":").map do |bit|
            "'#{bit}'"
          end.join(", ") + ")"
        end.join(", ")

        conn.execute("INSERT INTO search_results VALUES #{insert_part}")
      end
    end

    def drop_search_results_table
      ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS search_results")

论坛徽章:
0
2 [报告]
发表于 2011-06-23 13:25 |只看该作者
search_results (id INT AUTO_INCREMENT UNIQUE KEY, result_type VARCHAR(255)
INSERT INTO search_results VALUES (NULL, '0.09232217')
自增型字段就不要写在insert语句中了。

论坛徽章:
0
3 [报告]
发表于 2011-06-23 13:26 |只看该作者
SQL都没学会,先去学学吧。

论坛徽章:
0
4 [报告]
发表于 2011-06-23 21:17 |只看该作者
应该明白道理了啊。

论坛徽章:
0
5 [报告]
发表于 2011-06-25 10:50 |只看该作者
本帖最后由 bsxsb 于 2011-06-25 11:00 编辑
search_results (id INT AUTO_INCREMENT UNIQUE KEY, result_type VARCHAR(255)
INSERT INTO search_resul ...
2gua 发表于 2011-06-23 13:25


但是错误提示的是列数不匹配,

search_results (id INT AUTO_INCREMENT UNIQUE KEY, result_type VARCHAR(255), result_id INT)

最后的结果少了第三例  result_id,应该不是因为自增字段引起的错误。

还有,我承认我是sql的初学者,请不吝赐教,谢谢。

论坛徽章:
0
6 [报告]
发表于 2011-06-25 14:23 |只看该作者
再说一次,自增型字段不能写在insert语句中。

论坛徽章:
0
7 [报告]
发表于 2011-06-25 20:55 |只看该作者
回复 6# 2gua

麻烦您看一下下面的,尤其是红色的部分,完全没有问题,我不清楚您为什么一直在强调 自增行不能放在insert语句中,还有,我问题的关键也不在那,现在返回的确实少了一列,是少了result_id那一列,我现在觉得我的sql语句没有问题,不知到为什么少了一列,如果你真的能看出来问题出在那,麻烦你说一下吧,先不讨论能不能把自增的放在insert语句中,谢谢了!
mysql> INSERT INTO search_results VALUES (NULL, '0.09232217',1);
Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO search_results VALUES (NULL, '0.09232217',1);
Query OK, 1 row affected (0.00 sec)

mysql> show columns from search_results;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra    |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| result_type | varchar(255) | YES  |     | NULL    |                |
| result_id   | int(11)      | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> select * from search_results;
+----+-------------+-----------+
| id | result_type | result_id |
+----+-------------+-----------+
|  1 | 0.09232217  |         1 |
|  2 | 0.09232217  |         1 |
+----+-------------+-----------+

2 rows in set (0.00 sec)

论坛徽章:
0
8 [报告]
发表于 2011-06-26 07:03 |只看该作者
关闭。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP