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

SQLite - load a game from games, that has not been processed - AI

<p>Im just going add some of these here as they might be useful in the future.</p>
<p>Request:</p>
<div>
<div>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 gameuid 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. gameuid should equal uid in the games table.</div>
<div>&nbsp;</div>
<div>Response:</div>
<div>
<p>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:</p>
<p><strong>Query 1</strong> - Selects the latest game (by <code>insdate</code>) from the <code>games</code> table where the game's <code>uid</code> doesn't already exist as <code>gameuid</code> in the <code>gamenewslog</code> table.</p>
<p><strong>Query 2</strong> - Inserts the game details into the <code>gamenewslog</code> table with <code>processed = true</code> (represented as <code>1</code> in SQLite). This uses the same logic to find the latest unprocessed game.</p>
<p><strong>Query 3</strong> - 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.</p>
<p>Key points about these queries:</p>
<ol>
<li>I used <code>NOT IN</code> with a subquery to exclude games that already exist in <code>gamenewslog</code></li>
<li>The <code>processed</code> field is set to <code>1</code> (SQLite's representation of boolean <code>true</code>)</li>
<li>I set <code>insdate</code> to the current timestamp using <code>datetime('now')</code> for the new gamenewslog entry</li>
<li>The <code>uid</code> field in gamenewslog is set to <code>NULL</code> assuming it's auto-increment</li>
<li>I mapped <code>game
uid</code> to the <code>uid</code> from the games table as requested</li>
</ol>
<p>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.</p>
<p>&nbsp;</p>
</div>
</div>

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
5
This Month
100
This Year
311

No Items Found.

Add Comment
Type in a Nick Name here
 
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


James Clear
Latest News
## 🚀 AI Giants Hit Bullseye: Anthropic & OpenAI Achieve Product-Market Fit Anthropic and OpenAI have reached a significant milestone, finding product-market fit with their AI technologies, which means their products effectively meet the needs of their customers, driving growth and adoption. This achievement showcases the practical value of their innovations, enabling businesses and individuals to leverage AI for enhanced productivity and efficiency. With this alignment of product and market needs, these companies are poised to transform industries and shape the future of technology.