首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL连接器Python:插入失败

MySQL连接器Python:插入失败
EN

Stack Overflow用户
提问于 2017-01-03 17:08:05
回答 0查看 575关注 0票数 0

我正在尝试通过python脚本将值INSERT到MySQL表中。但是,我得到以下错误:

代码语言:javascript
复制
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like) VALUES ('0', '1', 'https://twitter.com/LesEchos/status/815950910832705538'' at line 1

我寻找了其他答案中建议的额外/缺失的空格,但找不到错误。我还使用相同的方式在其他表中插入值...你会发现下面是完整的代码:

代码语言:javascript
复制
import mysql.connector
import datetime

cnx = mysql.connector.connect(user='root',
                              host='localhost',
                              database='realcv')

cursor = cnx.cursor()
creation_date = datetime.datetime.now()

add_content = ("INSERT INTO CONTENT_TWITTER "
               "(id_content, id_account, url, is_active, creation_date, text, retweet, like) "
               "VALUES (%s, %s, %s, %s, %s, %s, %s, %s)")

id_account = ['1', '2', '3', '5', '6', '6', '6']
url = ['https://twitter.com/LesEchos/status/815950910832705538',
       'https://twitter.com/franceinter/status/815950791131283457',
       'https://twitter.com/LeHuffPost/status/815949875481378816',
       'https://twitter.com/lemondefr/status/815945788979290113',
       'https://twitter.com/frenchweb/status/815944495103496193',
       'https://twitter.com/frenchweb/status/815988537426378752',
       'https://twitter.com/frenchweb/status/815980996118114111']
is_active = ['1', '1', '1', '1', '1', '1', '0']
text = ["Les grands magasins Printemps signent à leur tour un accord sur l'ouverture du dimanche http:/trib.al/zIVr5id ",
        "L'uritrottoir c'est l'urinoir en forme de jardinière qui transforme son contenu en engrais ► http:/bit.ly/urinoir-rue  cc @MoreauEmm",
        "Mort du skieur Jean Vuarnet, champion olympique qui a donné son nom à la marque de lunettes",
        "« Les trois piliers de l’islam », une leçon de lecture du Coran pour les djihadistes",
        "[Étude de Cas] Comment Auchan.fr augmente ses ventes de +2,5% en personnalisant l’expérience de ses utilisateurs http:/bit.ly/2hQDeIh ",
        "#Automobile : les 4 tendances #IoT à venir à horizon 2020 http:/bit.ly/2hBfgEs ",
        "AAAAAAAAAAAA"]
retweet = ['20', '30', '18', '45', '27', '12', '5']
like = ['10', '25', '30', '12', '17', '54', '3']
count = 0
for i in range(len(id_account)):
    data_content = (str(count), id_account[i], url[i], is_active[i], creation_date, text[i], retweet[i], like[i])
    cursor.execute(add_content, data_content)
    count += 1

# Commit changes
cnx.commit()
cnx.close()

我尝试写入的表定义如下:

代码语言:javascript
复制
CREATE TABLE IF NOT EXISTS `realcv`.`CONTENT_TWITTER` (
  `id_content` DOUBLE NOT NULL,
  `id_account` DOUBLE NULL,
  `url` VARCHAR(100) NULL,
  `is_active` TINYINT(1) NULL,
  `creation_date` DATETIME NULL,
  `text` MEDIUMTEXT NULL,
  `retweet` INT NULL,
  `like` INT NULL,
  PRIMARY KEY (`id_content`),
  INDEX `account_content_idx` (`id_account` ASC),
  CONSTRAINT `account_content`
    FOREIGN KEY (`id_account`)
    REFERENCES `realcv`.`ACCOUNT` (`id_account`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41439917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档