π How to Integrate Gemini AI with Oracle APEX (Step-by-Step Guide)
In todayβs world, users donβt want to write complex SQL queriesβthey want instant answers in plain English.
What if your Oracle APEX application could understand natural language and automatically generate SQL queries, reports, and insights?
In this blog, Iβll show you how to connect Google Gemini AI with Oracle APEX and build a powerful AI-powered search engine π₯
π‘ What We Are Building
π A smart APEX application where users can:
- Ask questions in plain English
- Automatically generate Oracle SQL
- View results in reports/charts
- No SQL knowledge required
π§ Architecture Overview
User Input β Gemini AI API β SQL Query β Oracle APEX Report
π Step 1: Get Gemini API Key (Free)
- Go to Google AI Studio
- Login with your Google account
- Click Create API Key
- Copy the key
π No credit card required β
βοΈ Step 2: Create APEX Page Items
Create these items:
P1_SEARCH β User inputP1_RESPONSE β Store API responseP1_SQL β Extracted SQL
π Step 3: Call Gemini API using PL/SQL
Use APEX_WEB_SERVICE to call the API:
DECLARE
l_response CLOB;
BEGIN
apex_web_service.g_request_headers.delete;
apex_web_service.g_request_headers(1).name := 'x-goog-api-key';
apex_web_service.g_request_headers(1).value := 'YOUR_API_KEY';
apex_web_service.g_request_headers(2).name := 'Content-Type';
apex_web_service.g_request_headers(2).value := 'application/json';
l_response := apex_web_service.make_rest_request(
p_url => 'https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:generateContent',
p_http_method => 'POST',
p_body => '{
"contents": [
{
"parts": [
{
"text": "You are an Oracle SQL expert. Return ONLY SQL query. Table: XXQST_INVENTORY (ITEM_NAME, PRICE, QUANTITY, CATEGORY). User Query: ' || :P1_SEARCH || '"
}
]
}
]
}'
);
:P1_RESPONSE := l_response;
END;
π Step 4: Extract SQL from Response
Gemini returns JSON, so we extract SQL:
DECLARE
l_sql VARCHAR2(4000);
BEGIN
l_sql := REGEXP_SUBSTR(:P1_RESPONSE, 'SELECT.*', 1, 1);
:P1_SQL := l_sql;
END;
π Step 5: Display Data in APEX Report
Create an Interactive Report
Source:
RETURN :P1_SQL;
π― Example
User Input:
Show products where price > 5000
AI Output:
SELECT * FROM XXQST_INVENTORY WHERE PRICE > 5000;
π Report auto-generates instantly π
π‘ Pro Tips (Very Important)
π₯ 1. Use Strong Prompt
Return ONLY SQL query.
Do NOT explain.
No markdown.
π₯ 2. Define Table Structure
Always pass table + columns to AI
π₯ 3. Handle Errors
- Invalid SQL
- Empty responses
- API limits
β οΈ Common Issues
| Issue | Solution |
|---|
| 404 error | Use correct model |
| No response | Check API key |
| Wrong SQL | Improve prompt |
π₯ Final Result
You now have:
β
AI-powered search
β
Natural language β SQL
β
Auto reports
β
Zero SQL dependency
π Whatβs Next?
You can extend this further:
- π Auto charts & dashboards
- π€ Voice-based search
- π Multi-language queries
- π KPI cards
π¬ Final Thoughts
Integrating AI with Oracle APEX opens a new world of possibilities.
Youβre no longer building just appsβyouβre building intelligent systems.
π This is how modern Business Intelligence should feel.
π₯ If you found this helpful...
Follow for more Oracle APEX + AI content π
And feel free to connect!
#OracleAPEX #AI #Gemini #SQL #LowCode #Automation #DataAnalytics