I am looking for a way to insert rows that don't already exist. I have read about unique keys, i also added a id (unique auto inc) column. And i have tried alot of different methods - but i can't get it to work.
What i tried so far:
INSERT INTO programmes (station_id, programme_start, programme_end, title, subtitle, long_description, short_description, rating, imdb, episode, season, year, categories, icon, actors, writers, directors)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
station_id = VALUES(station_id),
programme_start = VALUES(programme_start)
i also tried:
ON DUPLICATE KEY UPDATE
but i keep getting #1064
errors saying there's something wrong at ''
I dont know what i am doing wrong.. I am using the database to update tv-guide information daily - and i get these data from different api's. Now these api responses sometimes contain dubbles (if a program spans over 2 days).
I also tried to use if and else
but everything i try fails with error #1064
When i try this query:
INSERT INTO `programmes` (`station_id`, `programme_start`, `programme_end`, `title`, `subtitle`, `long_description`, `short_description`, `rating`, `imdb`, `episode`, `season`, `year`, `categories`, `icon`, `actors`, `writers`, `directors`)
VALUES (1, '2018-10-16 00:00:00', '2018-10-16 00:20:00', 'NOS Journaal', '', 'Met het laatste nieuws, gebeurtenissen van nationaal en internationaal belang en de weersverwachting voor vandaag.', 'Met het laatste nieuws, gebeurtenissen van nationaal en internationaal belang en de weersverwachting voor vandaag.', '', '', '0', '0', '2017', 'Nieuws,', 'https://wp18-images-nl-dynamic.horizon.tv/EventImages/36652793.l.3dc8d9a6db753ad283c5909e6b29a7e61fb51d51.jpg', '', '', '')
ON DUPLICATE KEY UPDATE title = 'Test UPDATE'
it say's it inserted, but this row already exists, so it should be updated instead of inserting a new one.
My database contains several tables: - Providers (contains all the tv-guide / tv providers) - Stations (all tv stations) the id of the station is linked to the programmes table, so i can find all programmes for a station. - programmes (contains all programmes for all stations).
My programmes table is made with this query:
CREATE TABLE programmes (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
station_id INT NOT NULL,
programme_start DATETIME NOT NULL,
programme_end DATETIME NOT NULL,
title TEXT NOT NULL,
subtitle TEXT NOT NULL,
long_description TEXT NOT NULL,
short_description TEXT NOT NULL,
rating VARCHAR(255) NOT NULL,
imdb VARCHAR(255) NOT NULL,
episode INT NOT NULL,
season INT NOT NULL,
year VARCHAR(255) NOT NULL,
categories TEXT NOT NULL,
icon TEXT NOT NULL,
actors TEXT NOT NULL,
writers TEXT NOT NULL,
directors TEXT NOT NULL
);
the providers table and the stations table use the unique id as reference. This way i can find all stations for provider id=1, or id=2, and this way i can get all programmes for station id=1, or id=2 etc.