bsxsb 发表于 2011-06-23 08:10

求助 搜索问题 Mysql::Error: Column count doesn't match value count at row 1:

用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")

2gua 发表于 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语句中了。

2gua 发表于 2011-06-23 13:26

SQL都没学会,先去学学吧。

2gua 发表于 2011-06-23 21:17

应该明白道理了啊。

bsxsb 发表于 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 http://bbs.chinaunix.net/images/common/back.gif

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

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

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

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

2gua 发表于 2011-06-25 14:23

再说一次,自增型字段不能写在insert语句中。

bsxsb 发表于 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)

2gua 发表于 2011-06-26 07:03

关闭。
页: [1]
查看完整版本: 求助 搜索问题 Mysql::Error: Column count doesn't match value count at row 1: