Posted in sqlite
129
12:43 am, August 20, 2025
 

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:

Can you write a sqlite query to select the latest game from this table using insdate as the datetime structure of tables is below where the game_uid does not already exist in the gamenewslog table. Then can we add the details of the game into the gamenewslog table and mark it as processed = true. game_uid should equal uid in the games table.
 
Response:

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:

  1. I used NOT IN with a subquery to exclude games that already exist in gamenewslog
  2. The processed field is set to 1 (SQLite's representation of boolean true)
  3. I set insdate to the current timestamp using datetime('now') for the new gamenewslog entry
  4. The uid field in gamenewslog is set to NULL assuming it's auto-increment
  5. I mapped game_uid to the uid 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;

View Statistics
This Week
1
This Month
1
This Year
129

No Items Found.

Add Comment
Type in a Nick Name here
 
Related Search Terms
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote

Do you want to be right or happy? You can’t be both.

Suggests that in many situations - especially in relationships or arguments - insisting on being right can come at the cost of harmony or peace of mind. It implies that prioritizing your ego or the need to win an argument may damage relationships or personal contentment, whereas letting go of that need (even if you’re technically right) may lead to greater happiness.


Gerald Jampolsky
Random CSS Property

text-align

The text-align CSS property sets the horizontal alignment of the content inside a block element or table-cell box. This means it works like vertical-align but in the horizontal direction.
text-align css reference