December 16, 2021

About API (Application Programming Interface)

 An API (Application Programming Interface) is a set of rules and protocols that allows one software application to communicate with another. It defines the methods and data formats that programs can use to request and exchange information. APIs enable different systems, services, or applications to interact with each other, facilitating integration, data exchange, and feature access.

Key Aspects of an API:

  1. Requests and Responses: APIs allow one application to send a request to another application and receive a response. For example, a weather application might send a request to an external weather API to retrieve current weather data and receive the information in a structured format (like JSON or XML).
  2. Endpoints: An API typically has multiple "endpoints," which are specific URLs or URIs where different operations or functionalities are available. Each endpoint corresponds to a particular function, such as retrieving user information or processing a payment.
  3. Methods: The API uses different HTTP methods, such as:
    • GET: Retrieve data
    • POST: Send data to create something
    • PUT: Update existing data
    • DELETE: Remove data
  4. Authentication: Many APIs require authentication to ensure that the requester has permission to access the data or services. This can be done through methods like API keys, OAuth tokens, or other security protocols.

Types of APIs:

  • REST API: A common type of API that uses HTTP requests and follows REST principles (stateless, client-server communication).
  • SOAP API: A protocol that uses XML for messaging and follows strict rules for communication.
  • GraphQL: An API query language that allows clients to request exact the data they need.
  • Webhooks: A type of API that allows an application to send real-time data to another application when a specific event occurs.

Example:

Imagine you're using an application like a travel booking site. When you search for flights, the app uses an API to request flight data from an airline's database. The API will return the available flights, and the app will display that data to you in a user-friendly format. The API ensures that the app can interact with the airline’s system without needing to know the specifics of how the airline’s database is structured.

Benefits of APIs:

  • Interoperability: APIs allow different systems, even those built with different technologies, to communicate.
  • Efficiency: They enable faster development by allowing developers to use existing services and data.
  • Security: APIs can be secured with authentication mechanisms, ensuring that only authorized applications can access data.

In summary, APIs act as a bridge that allows applications to communicate and share data or functionality, streamlining processes and improving the development of web and mobile apps.

June 13, 2021

CSV to SQL Server

Script to Import Data from CSV to SQL Table

Script to Import Data from CSV to SQL Table

CSV File Location:

Traffic census data

Table Structure:


CREATE TABLE dbo.tbl_traffic_census(
    Col_1 int NULL, Col_2 int NULL, Col_3 int NULL,
    Col_4 int NULL, Col_5 int NULL, Col_6 int NULL,
    Col_7 int NULL, Col_8 int NULL, Col_9 int NULL,
    Col_10 int NULL, device_id varchar(255) NOT NULL,
    lane_number varchar(255) NOT NULL,
    cross_datetime datetime NOT NULL,
    speed_kmph_39 int NULL, speed_kmph_40_49 int NULL,
    speed_kmph_50_59 int NULL, speed_kmph_60_69 int NULL,
    speed_kmph_70_79 int NULL, speed_kmph_80_89 int NULL,
    speed_kmph_90_99 int NULL, speed_kmph_100_109 int NULL,
    speed_kmph_110_119 int NULL, speed_kmph_120_129 int NULL,
    speed_kmph_130_more int NULL,
    PRIMARY KEY CLUSTERED 
    (device_id ASC, lane_number ASC, cross_datetime ASC)
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
          IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
          ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) 
    ON PRIMARY
);
    

Python Code for Importing Data:

Alternatively, tools like the BCP command, import utility, or SSIS packages can be used for loading data from files into SQL tables.