简体   繁体   English

MySQL连接器Python:无法插入

[英]MySQL connector Python: fail to insert

I am trying to INSERT values into a MySQL table via a python script. 我试图INSERT通过python脚本值到一个MySQL表。 However, I get the following error: 但是,出现以下错误:

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

I looked for extra/missing blank space as suggested in other answers but coult not find the error. 我按照其他答案中的建议寻找了多余的/缺少的空格,但是找不到错误。 I also inserted values in other tables using the same fashion... You will find bellow the whole code: 我还以相同的方式在其他表中插入了值...您会在下面看到整个代码:

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()

The table I try to write to is defined as follows: 我尝试写入的表定义如下:

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;

Like is a keyword use backticks for reserverd keyword 就像是一个关键字,对reserverd关键字使用反引号

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

Column name should not be a keyword. 列名不能是关键字。 But you can escape those reserve words as a column name. 但是您可以将那些保留字作为列名转义。

In your program, column names:"like" and "text" are keywords. 在您的程序中,列名:“ like”和“ text”是关键字。 You can escape those using back tick character (`) : 您可以使用反斜杠字符(`)逃脱这些字符:

While creating : 创建时:

CREATE TABLE IF NOT EXISTS CONTENT_TWITTER
  (
  id_content TEXT,
  id_account TEXT, 
  url TEXT,
  is_active TEXT,
  creation_date TEXT, 
  `text` TEXT, 
  retweet TEXT,
  `like` TEXT)

While adding/inserting : 在添加/插入时:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM