Python插入到mysql表不起作用

时间:2015-12-24 00:18:05

标签: python mysql

我有一个名为Games的mysql表,我试图存储我从metacritic API检索的数据。该表的架构如下。

CREATE TABLE `Games` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) NOT NULL,
  `metacriticURL` varchar(256) NOT NULL,
  `rlsDate` date DEFAULT NULL,
  `metacriticRating` varchar(16) DEFAULT NULL,
  `metacriticScore` varchar(16) DEFAULT NULL,
  `platform` varchar(16) NOT NULL,
  PRIMARY KEY (`id`)
)

尝试使用MySQLdb API向表中插入数据时出现以下错误:

  

OperationalError(1136,“列数与行的值计数不匹配   1" )

以下是相关代码:

db = MySQLdb.connect(host = "localhost", 
                     user = "root",
                     passwd = "secretpassword",
                     db = "GamesDB")
cur = db.cursor()
insertQuery = 'INSERT into Games (name,
                                  metacriticURL,
                                  rlsDate, 
                                  metacriticRating,
                                  metacriticScore,
                                  platform) VALUES (%s %s %s %s %s %s)'
insertData = ('Game Name', 'http://www.website.com/some/path', datetime.datetime.strptime('2015-11-05', "%Y-%m-%d"), 'M', '74', 'ps4')
cur.execute(insertQuery, insertData)

我正在进一步查看错误,并从上面看到错误消息字符串。我已经调试了好几天了,所有值在执行调用之前看起来都很好。我的insertData中有6个值,我在SQL查询字符串中声明了6列,所以我不确定列计数如何不匹配。我在上面的代码示例中为此帖添加了硬编码值。

任何见解/想法都将不胜感激!

2 个答案:

答案 0 :(得分:0)

您的InsertQuery%s上有误,应该写成:

insertQuery = "INSERT into Games (name, metacriticURL, rlsDate, metacriticRating, metacriticScore, platform) 
VALUES ('%s', '%s', '%s', '%s', '%s', '%s')"

答案 1 :(得分:0)

你试过这个(%s,%s,%s,%s,%s,%s)?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery-slimScroll/1.3.7/jquery.slimscroll.js"></script>

<link rel="stylesheet" href="styles.css">
<link href='https://fonts.googleapis.com/css?family=Libre+Baskerville|Lora|Maven+Pro|Merriweather|EB+Garamond|Open+Sans|Lato|Slabo+27px|Josefin+Slab' rel='stylesheet' type='text/css'>
<link rel='shortcut icon' href='img/favicon.ico' type='img/favicon.ico'>
<div class="announcements">
  <h2>Announcements</h2>
  <div class="sectionwrapper" id="container">
  <div class="section">
    <span class="date">12/20/15</span><br><span class="important">!</span>
    <span class="new-policy"> New Policy </span>
    <a href="#"><span class="description">&nbsp;&nbsp;Paid Week Time Off each Month  <br>Every month, choose any week for paid time off for that entire week.  Spend time with your loved ones.  Go travel som...</span></a>
  </div>
  <hr>
  <div class="section">
        <span class="date">12/27/15</span><br><span class="important">!</span>
    <span class="new-policy"> New Policy </span>
    <a href="#"><span class="description">&nbsp;&nbsp;Paid Week Time Off each Month  <br>Every month, choose any week for paid time off for that entire week.  Spend time with your loved ones.  Go travel som...</span></a>
  </div>
  <hr>


  <div class="section">
        <span class="date">12/20/15</span><br><span class="important">!</span>
    <span class="new-policy"> New Policy </span>
    <a href="#"><span class="description">&nbsp;&nbsp;Paid Week Time Off each Month  <br>Every month, choose any week for paid time off for that entire week.  Spend time with your loved ones.  Go travel som...</span></a>
  </div>
  <hr>

  <div class="section">
            <span class="date">12/20/15</span><br><span class="important">!</span>
    <span class="new-policy"> New Policy </span>
    <a href="#"><span class="description">&nbsp;&nbsp;Paid Week Time Off each Month  <br>Every month, choose any week for paid time off for that entire week.  Spend time with your loved ones.  Go travel som...</span></a>
  </div>
  <hr>
    <div class="section">
            <span class="date">12/20/15</span><br><span class="important">!</span>
    <span class="new-policy"> New Policy </span>
    <a href="#"><span class="description">&nbsp;&nbsp;Paid Week Time Off each Month  <br>Every month, choose any week for paid time off for that entire week.  Spend time with your loved ones.  Go travel som...</span></a>
  </div>
  <hr>
  </div>
  </div>
</div>