Skip to content

This is a Streamlit-based application that converts natural language questions into SQL queries and retrieves data from a SQLite database.

Notifications You must be signed in to change notification settings

mstfgul/Retrieve-SQL-Data-via-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ—ƒοΈ Gemini SQL Query App (Streamlit + Google Generative AI)

This is a Streamlit-based application that converts natural language questions into SQL queries and retrieves data from a SQLite database. It uses:

  • πŸ€– Google Generative AI (Gemini 2.0 Flash) for natural language to SQL conversion
  • πŸ—„οΈ SQLite for database operations
  • πŸ–₯️ Streamlit for the web interface
  • 🎯 Smart prompt engineering for accurate SQL generation

πŸš€ Setup Instructions

  1. Clone the repository (if you haven't yet)
git clone https://github.com/yourusername/your-repo-name.git
cd your-repo-name
  1. Install dependencies:
pip install -r requirements.txt
  1. Create a .env file in the project root and add your API key:
GOOGLE_API_KEY=your_google_api_key
  1. Prepare your SQLite database:
    • Ensure you have a student.db file in your project root
    • The database should contain a STUDENT table with columns: NAME, CLASS, SECTION, MARKS

πŸ“Š Database Schema

The application expects a SQLite database with the following structure:

CREATE TABLE STUDENT (
    NAME TEXT,
    CLASS TEXT,
    SECTION TEXT,
    MARKS INTEGER
);

▢️ How to Run

Start the Streamlit app:

streamlit run app.py

🧠 How It Works

  1. Enter your question in natural language (e.g., "How many students are in Data Science class?")
  2. The app uses Google Gemini to convert your question into a SQL query
  3. The generated SQL query is executed against the SQLite database
  4. Results are displayed in the Streamlit interface

πŸ’‘ Example Queries

  • "How many entries of records are present?"

    • Generates: SELECT COUNT(*) FROM STUDENT
  • "Tell me all the students studying in Data Science class?"

    • Generates: SELECT * FROM STUDENT WHERE CLASS = 'Data Science'
  • "Show me students with marks greater than 80"

    • Generates: SELECT * FROM STUDENT WHERE MARKS > 80

πŸ“Œ Features

  • βœ… Natural language to SQL conversion
  • βœ… Real-time query execution
  • βœ… Clean and intuitive web interface
  • βœ… Error handling for database operations
  • βœ… Smart prompt engineering for accurate results

πŸ“Œ Notes

  • The database file student.db must be present in the project root
  • Queries are limited to the STUDENT table structure
  • The app automatically handles SQL formatting and execution
  • No SQL injection protection - use only for trusted environments

πŸ”§ Requirements

streamlit
google-generativeai
python-dotenv
sqlite3

About

This is a Streamlit-based application that converts natural language questions into SQL queries and retrieves data from a SQLite database.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages