<reponame>paccy-ops/locationApi<filename>locations/data/create_db.py
import psycopg2


class CreateDatabase:

    def __init__(self):
        self.db = 'map_api'
        self.user = 'postgres'
        self.password = '<PASSWORD>'
        self.host = '127.0.0.1'
        self.port = '5432'
        self.message = 'Table created successfully........'

    def database_connection(self):
        """Establishing the connection"""
        conn = psycopg2.connect(database=self.db, user=self.user, password=self.password, host=self.host,
                                port=self.port)
        # Creating a cursor object
        cursor = conn.cursor()
        # Doping EMPLOYEE table if already exists.
        cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")

        """Creating table as per requirement"""
        commands = (
            """
            CREATE TABLE countries_data (
                country_id SERIAL PRIMARY KEY,
                country_name VARCHAR(100),
                location_cord VARCHAR(100),
                region_name  VARCHAR CHECK (length(region_name) <= 255),
                city_name VARCHAR CHECK (length(city_name) <= 255),
                keywords VARCHAR CHECK (length(keywords) <= 255)
            )
            """)

        cursor.execute(commands)
        # for s in commands:
        #     cursor.execute(s)
        print(self.message)
        conn.commit()
        # Closing the connection
        conn.close()


db = CreateDatabase()
db.database_connection()

import psycopg2


class CreateDatabase:

    def __init__(self):
        self.db = 'map_api'
        self.user = 'postgres'
        self.password = '<PASSWORD>'
        self.host = '127.0.0.1'
        self.port = '5432'
        self.message = 'Table created successfully........'

    def database_connection(self):
        """Establishing the connection"""
        conn = psycopg2.connect(database=self.db, user=self.user, password=self.password, host=self.host,
                                port=self.port)
        # Creating a cursor object
        cursor = conn.cursor()
        # Doping EMPLOYEE table if already exists.
        cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")

        """Creating table as per requirement"""
        commands = (
            """
            CREATE TABLE countries_data (
                country_id SERIAL PRIMARY KEY,
                country_name VARCHAR(100),
                location_cord VARCHAR(100),
                region_name  VARCHAR CHECK (length(region_name) <= 255),
                city_name VARCHAR CHECK (length(city_name) <= 255),
                keywords VARCHAR CHECK (length(keywords) <= 255)
            )
            """)

        cursor.execute(commands)
        # for s in commands:
        #     cursor.execute(s)
        print(self.message)
        conn.commit()
        # Closing the connection
        conn.close()


db = CreateDatabase()
db.database_connection()
Establishing the connection # Creating a cursor object # Doping EMPLOYEE table if already exists. Creating table as per requirement CREATE TABLE countries_data (                 country_id SERIAL PRIMARY KEY,                 country_name VARCHAR(100),                 location_cord VARCHAR(100),                 region_name  VARCHAR CHECK (length(region_name) <= 255),                 city_name VARCHAR CHECK (length(city_name) <= 255),                 keywords VARCHAR CHECK (length(keywords) <= 255)             ) # for s in commands: #     cursor.execute(s) # Closing the connection