SQLite - load a game from games, that has not been processed - AI
Im just going add some of these here as they might be useful in the future.
Request:
I'll help you create the SQLite queries for this task. Here are the two queries you need:I've created three different approaches for your SQLite queries:
Query 1 - Selects the latest game (by insdate
) from the games
table where the game's uid
doesn't already exist as game_uid
in the gamenewslog
table.
Query 2 - Inserts the game details into the gamenewslog
table with processed = true
(represented as 1
in SQLite). This uses the same logic to find the latest unprocessed game.
Query 3 - A more robust approach using a Common Table Expression (CTE) that ensures you're working with exactly the same record in both the selection and insertion operations.
Key points about these queries:
- I used
NOT IN
with a subquery to exclude games that already exist ingamenewslog
- The
processed
field is set to1
(SQLite's representation of booleantrue
) - I set
insdate
to the current timestamp usingdatetime('now')
for the new gamenewslog entry - The
uid
field in gamenewslog is set toNULL
assuming it's auto-increment - I mapped
game_uid
to theuid
from the games table as requested
The CTE approach (Query 3) is recommended if you want to ensure atomicity and avoid any potential race conditions between the selection and insertion operations.
PHP
-- Query 1: Select the latest game that doesn't exist in gamenewslog
SELECT *
FROM games g
WHERE g.uid NOT IN (
SELECT DISTINCT game_uid
FROM gamenewslog
WHERE game_uid IS NOT NULL
)
ORDER BY g.insdate DESC
LIMIT 1;
-- Query 2: Insert the game details into gamenewslog and mark as processed
INSERT INTO gamenewslog (
uid,
insdate,
title,
additional,
category,
md5,
results,
errors,
game_uid,
game_title,
processed
)
SELECT
NULL, -- uid (assuming auto-increment or will be generated)
datetime('now'), -- insdate (current timestamp)
g.title, -- title from games table
g.additional, -- additional from games table
g.category, -- category from games table
g.md5, -- md5 from games table
NULL, -- results (initially null)
NULL, -- errors (initially null)
g.uid, -- game_uid equals uid from games table
g.title, -- game_title
1 -- processed = true (using 1 for boolean true)
FROM games g
WHERE g.uid NOT IN (
SELECT DISTINCT game_uid
FROM gamenewslog
WHERE game_uid IS NOT NULL
)
ORDER BY g.insdate DESC
LIMIT 1;
-- Alternative approach using a variable (if your SQLite version supports it)
-- This ensures you're working with the same record in both operations:
WITH latest_game AS (
SELECT *
FROM games g
WHERE g.uid NOT IN (
SELECT DISTINCT game_uid
FROM gamenewslog
WHERE game_uid IS NOT NULL
)
ORDER BY g.insdate DESC
LIMIT 1
)
INSERT INTO gamenewslog (
uid,
insdate,
title,
additional,
category,
md5,
results,
errors,
game_uid,
game_title,
processed
)
SELECT
NULL, -- uid
datetime('now'), -- insdate
lg.title, -- title
lg.additional, -- additional
lg.category, -- category
lg.md5, -- md5
NULL, -- results
NULL, -- errors
lg.uid, -- game_uid
lg.title, -- game_title
1 -- processed = true
FROM latest_game lg;