2017-01-03 79 views
0

我想通過一個python腳本將INSERT值寫入MySQL表。不過,我得到以下錯誤:MySQL連接器Python:無法插入

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 

我找了額外/缺失的空白空間,在其他的答案建議,但coult找不到錯誤。我也用同樣的方式其他表插入值......你會發現婁整個代碼:

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

我試着寫的定義如下表:

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; 
+2

'like'可能是一個關鍵詞;您可能需要用反引號包裝字段名稱。 –

+0

你釘了它!如果你把它寫成答案,我會驗證它。 –

+0

它;建議不要使用關鍵字作爲變量名稱 – csharpcoder

回答

1

列名不應該是關鍵字。但是你可以將這些保留字作爲列名轉義。

在您的程序中,列名稱:「like」和「text」是關鍵字。您可以使用反勾字符逃生者(`):

在創建:

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) 

同時添加/插入:

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

喜歡的是一個關鍵字使用反引號存儲關鍵字

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