Syllabus →

Screenshot 2024-02-08 at 10.04.35 AM.png

Screenshot 2024-02-08 at 10.06.31 AM.png


Endsem PYQ →

Screenshot 2024-03-20 at 5.51.17 PM.png

Embedded SQL is a method of combining the computing power of a programming language with the database query capabilities of SQL (Structured Query Language). This integration allows developers to write SQL commands directly within the code of a host programming language, such as C, C++, Java, or COBOL. This approach simplifies database operations by enabling the seamless execution of SQL queries from within the host program.

Key Concepts of Embedded SQL

  1. Host Language: The programming language in which the SQL statements are embedded. Examples include C, C++, Java, and COBOL.
  2. Embedded SQL Statements: SQL statements that are written within the host language. These statements are usually marked by special syntax to distinguish them from the host language code.
  3. Preprocessor: A tool that processes the embedded SQL code. The preprocessor translates the embedded SQL statements into standard API calls of the host language, enabling the host program to interact with the database.
  4. Host Variables: Variables declared in the host language that can be used in SQL statements. They allow data to be exchanged between the SQL statements and the host program.

Example in C

Here's a simple example to demonstrate embedded SQL in a C program:

#include <stdio.h>
#include <sqlca.h>  // Include the SQL Communications Area header

int main() {
    EXEC SQL BEGIN DECLARE SECTION;
    char username[20];
    char password[20];
    EXEC SQL END DECLARE SECTION;

    // Assign values to host variables
    strcpy(username, "admin");
    strcpy(password, "password123");

    // Connect to the database
    EXEC SQL CONNECT TO mydatabase USER :username IDENTIFIED BY :password;

    if (sqlca.sqlcode != 0) {
        printf("Connection failed: %s\\\\n", sqlca.sqlerrm.sqlerrmc);
        return 1;
    }

    // Execute a query
    EXEC SQL SELECT COUNT(*) INTO :count FROM users;

    // Disconnect from the database
    EXEC SQL DISCONNECT;

    printf("Number of users: %d\\\\n", count);

    return 0;
}

Advantages of Embedded SQL

  1. Efficiency: Embedded SQL allows direct execution of SQL queries from the host program, reducing the overhead of external query processing.
  2. Convenience: It simplifies coding by allowing database operations to be performed within the same environment as the application logic.
  3. Strong Typing: Host variables provide strong typing and help prevent SQL injection attacks by ensuring that data types are consistent.