<reponame>zzhmark/ccf-db-backend<gh_stars>0
#!/usr/bin/python
import psycopg2
from .config import config  # 这是上面的config()代码块，已经保存在config.py文件中


def query(que='SELECT version()'):
    """ Connect to the PostgreSQL database server """
    conn = None
    output = []
    try:
        # read connection parameters
        params = config()

        # connect to the PostgreSQL server
        print('Connecting to the PostgreSQL database...')
        conn = psycopg2.connect(**params)

        # create a cursor
        cur = conn.cursor()

        # execute a statement
        cur.execute(que)

        # display the PostgreSQL database server version
        output = {"records": cur.fetchall(), 'colnames': [i[0] for i in cur.description]}
        # close the communication with the PostgreSQL
        cur.close()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()
            print('Database connection closed.')
        return output


def modify(commands):
    conn = None
    try:
        # read the connection parameters
        params = config()
        # connect to the PostgreSQL server
        conn = psycopg2.connect(**params)
        cur = conn.cursor()
        # create table one by one
        for command in commands:
            cur.execute(command)
        # close communication with the PostgreSQL database server
        cur.close()
        # commit the changes
        conn.commit()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()


#!/usr/bin/python
import psycopg2
from .config import config  # 这是上面的config()代码块，已经保存在config.py文件中


def query(que='SELECT version()'):
    """ Connect to the PostgreSQL database server """
    conn = None
    output = []
    try:
        # read connection parameters
        params = config()

        # connect to the PostgreSQL server
        print('Connecting to the PostgreSQL database...')
        conn = psycopg2.connect(**params)

        # create a cursor
        cur = conn.cursor()

        # execute a statement
        cur.execute(que)

        # display the PostgreSQL database server version
        output = {"records": cur.fetchall(), 'colnames': [i[0] for i in cur.description]}
        # close the communication with the PostgreSQL
        cur.close()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()
            print('Database connection closed.')
        return output


def modify(commands):
    conn = None
    try:
        # read the connection parameters
        params = config()
        # connect to the PostgreSQL server
        conn = psycopg2.connect(**params)
        cur = conn.cursor()
        # create table one by one
        for command in commands:
            cur.execute(command)
        # close communication with the PostgreSQL database server
        cur.close()
        # commit the changes
        conn.commit()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()
#!/usr/bin/python # 这是上面的config()代码块，已经保存在config.py文件中 Connect to the PostgreSQL database server # read connection parameters # connect to the PostgreSQL server # create a cursor # execute a statement # display the PostgreSQL database server version # close the communication with the PostgreSQL # read the connection parameters # connect to the PostgreSQL server # create table one by one # close communication with the PostgreSQL database server # commit the changes