INSERT SELECT * FROM ON DUPLICATE导致错误1136

时间:2015-12-26 00:32:19

标签: mysql python-2.7 pandas bulkinsert on-duplicate-key

我正在尝试将df插入/更新到mysql表中。我收到此错误

  

“_ mysql_exceptions.OperationalError:(1136,”列数与第1行的值计数不匹配)“

在下面代码中的执行行上。有关变通方法或更好方法的任何建议吗?

db = MySQLdb.connect(
  host = "myhost",
  user = "username",
  passwd = "password",
  db = "mydb",
  charset='utf8'
 )

df.to_sql(con=db, name='t', if_exists='replace', flavor='mysql')

sqlStatement = """INSERT INTO db.films SELECT t.* FROM db.t
            ON DUPLICATE KEY UPDATE 
            films.releasedP=t.releasedP,
            films.runtimeP=t.runtimeP,
            films.imdbRatingP=t.imdbRatingP,
            films.votesYearP=t.votesYearP,
            films.years=t.years,
            films.votesYear=t.votesYear
            ;"""

cursor = db.cursor()
cursor.execute(sqlStatement)
db.commit()

这是我尝试插入/更新数据的表的架构。

DESCRIBE db.t;

+----------------------------+-------------+------+-----+---------+-------+
| Field                      | Type        | Null | Key | Default | Extra |
+----------------------------+-------------+------+-----+---------+-------+
| Title                      | varchar(63) | YES  |     | NULL    |       |
| imdbID                     | varchar(63) | YES  |     | NULL    |       |
| Released                   | varchar(63) | YES  |     | NULL    |       |
| Runtime                    | varchar(63) | YES  |     | NULL    |       |
| imdbRating                 | varchar(63) | YES  |     | NULL    |       |
| imdbVotes                  | varchar(63) | YES  |     | NULL    |       |
| releasedDateTime           | datetime    | YES  |     | NULL    |       |
| maxDateTime                | datetime    | YES  |     | NULL    |       |
| years                      | float       | YES  |     | NULL    |       |
| votesYear                  | float       | YES  |     | NULL    |       |
| rating                     | float       | YES  |     | NULL    |       |
| runtimeF                   | float       | YES  |     | NULL    |       |
| releasedP                  | float       | YES  |     | NULL    |       |
| runtimeP                   | float       | YES  |     | NULL    |       |
| imdbRatingP                | float       | YES  |     | NULL    |       |
| votesYearP                 | float       | YES  |     | NULL    |       |
+----------------------------+-------------+------+-----+---------+-------+

DESCRIBE db.films;

+----------------------------+-------------+------+-----+---------+-------+
| Field                      | Type        | Null | Key | Default | Extra |
+----------------------------+-------------+------+-----+---------+-------+
| Actors                     | varchar(63) | YES  |     | NULL    |       |
| Awards                     | varchar(63) | YES  |     | NULL    |       |
| Country                    | varchar(63) | YES  |     | NULL    |       |
| Director                   | varchar(63) | YES  |     | NULL    |       |
| Genre                      | varchar(63) | YES  |     | NULL    |       |
| Language                   | varchar(63) | YES  |     | NULL    |       |
| Metascore                  | varchar(63) | YES  |     | NULL    |       |
| Plot                       | varchar(63) | YES  |     | NULL    |       |
| Poster                     | varchar(63) | YES  |     | NULL    |       |
| Rated                      | varchar(63) | YES  |     | NULL    |       |
| Released                   | varchar(63) | YES  |     | NULL    |       |
| Response                   | varchar(63) | YES  |     | NULL    |       |
| Runtime                    | varchar(63) | YES  |     | NULL    |       |
| Title                      | varchar(63) | YES  |     | NULL    |       |
| Type                       | varchar(63) | YES  |     | NULL    |       |
| Writer                     | varchar(63) | YES  |     | NULL    |       |
| Year                       | varchar(63) | YES  |     | NULL    |       |
| imdbID                     | varchar(63) | YES  | UNI | NULL    |       |
| imdbRating                 | varchar(63) | YES  |     | NULL    |       |
| imdbVotes                  | varchar(63) | YES  |     | NULL    |       |
| years                      | float       | YES  |     | NULL    |       |
| votesYear                  | float       | YES  |     | NULL    |       |
| rating                     | float       | YES  |     | NULL    |       |
| runtimeF                   | float       | YES  |     | NULL    |       |
| releasedP                  | float       | YES  |     | NULL    |       |
| runtimeP                   | float       | YES  |     | NULL    |       |
| imdbRatingP                | float       | YES  |     | NULL    |       |
| votesYearP                 | float       | YES  |     | NULL    |       |
| releasedDateTime           | datetime    | YES  |     | NULL    |       |
| maxDateTime                | datetime    | YES  |     | NULL    |       |
+----------------------------+-------------+------+-----+---------+-------+

1 个答案:

答案 0 :(得分:2)

db.filmsdb.t的模式不匹配。所以你需要定义表db.films的哪些列将被设置为表``的值。

尝试此查询:

INSERT INTO db.films (
  Title,
  imdbID,
  Released,
  Runtime,
  imdbRating,
  imdbVotes,
  releasedDateTime,
  maxDateTime,
  years,
  votesYear,
  rating,
  runtimeF,
  releasedP,
  runtimeP,
  imdbRatingP,
  votesYearP
)
SELECT
  Title,
  imdbID,
  Released,
  Runtime,
  imdbRating,
  imdbVotes,
  releasedDateTime,
  maxDateTime,
  years,
  votesYear,
  rating,
  runtimeF,
  releasedP,
  runtimeP,
  imdbRatingP,
  votesYearP
FROM db.t
ON DUPLICATE KEY UPDATE 
  films.releasedP=t.releasedP,
  films.runtimeP=t.runtimeP,
  films.imdbRatingP=t.imdbRatingP,
  films.votesYearP=t.votesYearP,
  films.years=t.years,
  films.votesYear=t.votesYear