Skip to main content

Friday 17th January 2025 – Leveraging Claude and Ollama to query my local database

By January 17, 2025January 18th, 2025AI, Web Development

Today I had a look at Claude (https://claude.ai/)
I already have an account with Claude. 
I asked Claude: ‘How can I use AI to access my local mysql database’
Claude wrote me a Python script to leverage OpenAI complete with installation instructions.
I decided that I would prefer the script to be in PHP.
I therefore asked Claude: ‘can you convert the above python script into php?’
Clause obliged (again with full installation instructions). 
I then set up an account with OpenAI. To be able to make any API calls I was required to deposit a payment. I deposited $5 (U.S.)
I configured my local database details and my OpenAI API key.
I edited the script and for the prompt I put: ‘Show me all users’
Running this script (using Laragon – Herd would be an alternative) my browser displayed the SQL query as well as the users in the DB. OpenAI changed me under 1 cent for this call.
Unfortunately, when I asked OpenAI a slightly more complicated query it charged me 23 cents.
I therefore decided to go back to Claude and asked: ‘openai is quite expensive are there any free alternatives’

Claude suggested Ollama (https://ollama.ai/) and duly wrote me a modified PHP script with instructions.
The script suggested using the CodeLlama model. I could not get this to work and so asked Claude:
I am getting the error ‘SQL Query: Error: Ollama API Error: model ‘codellama’ not found’.
Claude suggested I use mistral and that worked.

I asked Claude: “Show me all posts by the user with the email address of ‘percy.gottlieb@example.net'”

 

Your Title Goes Here
Result returned

SQL Query: SELECT posts.* FROM users INNER JOIN posts ON users.id = posts.user_id WHERE users.email = ‘percy.gottlieb@example.net’; Results: Array ( [id] => 13 [user_id] => 29 [title] => Odio quas placeat harum sed quia. [content] => Commodi eius quasi consequatur inventore. Repellendus blanditiis fugiat debitis fugit ut. Fugit iste aut porro incidunt dolor est accusamus. Accusamus quo est in qui ipsum. Ducimus voluptatem incidunt illo sunt sit est. Sequi adipisci aut sit nisi qui. Autem velit veniam consequuntur debitis. Praesentium et accusamus debitis. Quo quam suscipit at aperiam dolores vero odit earum. Voluptas vitae repudiandae aut eos veniam. Itaque iusto delectus quia sed repellat quo ratione. Numquam nisi eos nisi inventore quo sint cupiditate est. Sed eius amet aperiam numquam est. [published] => 1 [created_at] => 2022-12-08 23:06:48 [updated_at] => 2022-12-08 23:06:48 ) Array ( [id] => 14 [user_id] => 29 [title] => Maiores est provident rerum. [content] => Et quaerat quia impedit modi ut aliquid. Voluptatem neque veritatis consectetur ut nesciunt ab. Quis alias autem reiciendis non architecto fugit nobis. Nisi expedita mollitia consectetur temporibus et voluptatem culpa ducimus. Saepe aut vel rerum velit et nihil. In quos magni magnam aut velit. Perspiciatis nesciunt facilis est vel sunt voluptates ut. Error reprehenderit eaque aperiam aperiam amet consequatur quos qui. Doloremque quaerat voluptatum amet. Autem voluptates explicabo dolorem ut est. Sit totam soluta quos. Aliquid ipsam voluptatem dolores quam ex. Laborum qui omnis quam aut et nostrum. Repellendus dolores cum consequatur dolor fugiat placeat numquam quibusdam. Atque eos mollitia doloremque quasi. Tenetur quia sint eligendi. Voluptas sapiente ducimus aliquid impedit blanditiis. Possimus omnis at architecto tempore rerum deleniti. Ea quidem sapiente quis. Et neque nihil unde accusamus autem. Et non sit id officiis quod qui delectus. Recusandae veritatis repellat cupiditate nisi enim. Dignissimos aut incidunt asperiores magnam quis in. Ea hic excepturi deleniti optio cupiditate. Ex recusandae delectus culpa labore nesciunt maxime distinctio. Amet quia animi accusantium aspernatur et magnam quidem. Aliquam impedit debitis qui beatae veniam vel voluptatem. [published] => 1 [created_at] => 2022-12-08 23:06:48 [updated_at] => 2022-12-08 23:06:48 ) Array ( [id] => 15 [user_id] => 29 [title] => Esse quisquam beatae non fugiat velit. [content] => Incidunt et beatae consectetur id repellendus illo. Id autem incidunt a voluptatem. Saepe a ut minima nihil est id in. Dolor occaecati assumenda est. Est id cumque dolor culpa. Ratione omnis magnam beatae. Eligendi aliquam error consequuntur minima aut nostrum vel amet. Quo corrupti vel similique consectetur adipisci et. Veniam nesciunt nam ipsum. Officiis voluptatum repudiandae ullam et. Fugiat aliquid id cupiditate et. Tenetur corporis voluptatem et fugit. [published] => 0 [created_at] => 2022-12-08 23:06:48 [updated_at] => 2022-12-08 23:06:48 ) Array ( [id] => 16 [user_id] => 29 [title] => Maiores pariatur quaerat quos a. [content] => Consequuntur ut asperiores sint nostrum incidunt sit. Velit adipisci dignissimos ut et eius est est. Officiis ullam maiores occaecati eum labore et in. Enim quia eveniet ratione voluptates ut sed et. Et vel dolor inventore aut ad. Blanditiis ea enim distinctio sint quibusdam. Perspiciatis rerum omnis occaecati. Tempora quo optio assumenda dolorem aut non. Qui dolorem temporibus laborum inventore. Natus vero quisquam sequi sapiente. Aut quos et repellendus reprehenderit eaque. Aperiam ducimus amet est rerum. Sit quos hic atque facere eius aut. Cumque qui ipsam consequuntur dolor officiis. Minima odio voluptas tempora voluptas recusandae. Doloremque natus enim molestiae dolorem modi explicabo. Tempora et omnis eligendi consequatur ab omnis. Earum quibusdam deserunt mollitia eveniet ratione perferendis. Repellendus deleniti voluptatum aut cumque in fugit. Non iure consequuntur eligendi sed occaecati nisi. A qui consequatur qui error et. Ullam itaque ratione est sit odio ipsam quisquam. Aspernatur consequatur ipsa vel. Et molestias et pariatur qui aut. Aut laborum perspiciatis cumque. Libero in omnis sit et reiciendis accusantium est. Ratione odio laboriosam consequuntur qui distinctio aperiam nobis. Et reiciendis incidunt doloribus voluptas sit dolor. Voluptas ratione tempore ut consequatur neque. Animi ut qui non officia quos similique ipsam quas. [published] => 1 [created_at] => 2022-12-08 23:06:48 [updated_at] => 2022-12-08 23:06:48 )

I then asked Claude: ‘display this array as a html table’ and pasted in the above array.
Claude returned:

Another prompt for Ollama: 

“Show me all users with more than 3 posts. Give me the name of the user and the total number of posts for each.”
Your Title Goes Here
Result

SQL Query: SELECT users.name, COUNT(posts.id) as total_posts FROM users JOIN posts ON users.id = posts.user_id GROUP BY users.id HAVING COUNT(posts.id) > 3; Results: Array ( [name] => Anabelle Wintheiser [total_posts] => 4 ) Array ( [name] => Karl Deckow [total_posts] => 4 ) Array ( [name] => Dallin Powlowski DDS [total_posts] => 4 ) Array ( [name] => Greyson Abshire [total_posts] => 4 ) Array ( [name] => Elsie Schneider V [total_posts] => 4 ) Array ( [name] => Ms. Janae Gottlieb Jr. [total_posts] => 4 ) Array ( [name] => Bernhard Ryan [total_posts] => 4 ) Array ( [name] => Jaime Abshire [total_posts] => 4 ) Array ( [name] => Manuel Wuckert [total_posts] => 4 ) Array ( [name] => Hilma Donnelly [total_posts] => 4 ) Array ( [name] => Prof. Elta Heidenreich [total_posts] => 4 ) Array ( [name] => Lupe Purdy [total_posts] => 4 ) Array ( [name] => Chadrick Schamberger IV [total_posts] => 4 ) Array ( [name] => Prof. Johnpaul Mueller [total_posts] => 4 ) Array ( [name] => Hilbert Schoen [total_posts] => 4 ) Array ( [name] => Prof. Heather Trantow [total_posts] => 4 ) Array ( [name] => Prof. Claud Marvin Sr. [total_posts] => 4 ) Array ( [name] => Dandre Douglas [total_posts] => 4 ) Array ( [name] => Janick Hilpert [total_posts] => 4 ) Array ( [name] => Prof. Jayde Krajcik Jr. [total_posts] => 4 ) Array ( [name] => Mollie Grant [total_posts] => 4 ) Array ( [name] => Duncan Cruickshank [total_posts] => 4 ) Array ( [name] => Miss Savannah Christiansen [total_posts] => 4 ) Array ( [name] => Mr. Talon Hane III [total_posts] => 4 ) Array ( [name] => Shemar Emmerich V [total_posts] => 4 )

Formatted in a table

“Show me all users with more than 0 published posts. Show me their name and the total of published posts”
Your Title Goes Here
Result

SQL Query: SELECT users.name, COUNT(posts.id) as total_published_posts FROM users JOIN posts ON users.id = posts.user_id WHERE posts.published = 1 GROUP BY users.id HAVING total_published_posts > 0; Results: Array ( [name] => Anabelle Wintheiser [total_published_posts] => 4 ) Array ( [name] => Karl Deckow [total_published_posts] => 2 ) Array ( [name] => Dallin Powlowski DDS [total_published_posts] => 2 ) Array ( [name] => Greyson Abshire [total_published_posts] => 3 ) Array ( [name] => Elsie Schneider V [total_published_posts] => 2 ) Array ( [name] => Ms. Janae Gottlieb Jr. [total_published_posts] => 2 ) Array ( [name] => Bernhard Ryan [total_published_posts] => 2 ) Array ( [name] => Jaime Abshire [total_published_posts] => 1 ) Array ( [name] => Manuel Wuckert [total_published_posts] => 2 ) Array ( [name] => Hilma Donnelly [total_published_posts] => 1 ) Array ( [name] => Prof. Elta Heidenreich [total_published_posts] => 4 ) Array ( [name] => Lupe Purdy [total_published_posts] => 2 ) Array ( [name] => Chadrick Schamberger IV [total_published_posts] => 1 ) Array ( [name] => Prof. Johnpaul Mueller [total_published_posts] => 2 ) Array ( [name] => Hilbert Schoen [total_published_posts] => 3 ) Array ( [name] => Prof. Heather Trantow [total_published_posts] => 2 ) Array ( [name] => Prof. Claud Marvin Sr. [total_published_posts] => 2 ) Array ( [name] => Dandre Douglas [total_published_posts] => 1 ) Array ( [name] => Janick Hilpert [total_published_posts] => 2 ) Array ( [name] => Prof. Jayde Krajcik Jr. [total_published_posts] => 1 ) Array ( [name] => Mollie Grant [total_published_posts] => 3 ) Array ( [name] => Duncan Cruickshank [total_published_posts] => 1 ) Array ( [name] => Miss Savannah Christiansen [total_published_posts] => 3 ) Array ( [name] => Mr. Talon Hane III [total_published_posts] => 3 ) Array ( [name] => Shemar Emmerich V [total_published_posts] => 2 )

Leave a Reply