代码改变世界

Python: 数据库访问库(3)

2022-04-27 21:28  huoit  阅读(20)  评论(0)    收藏  举报

psycopy2

 

def connect_db():
    try:
        conn = psycopg2.connect(database='postgres', user='postgres',password='root', host='127.0.0.1', port=5432)
    except Exception as e:
        error_logger.error(e)
    else:
        return conn
    return None

 

def close_db_connection(conn):
    conn.commit()
    conn.close()

def create_db():
    conn = connect_db()
    if not conn:
        return
    cur = conn.cursor()
    cur.execute(" CREATE TABLE IF NOT EXISTS dictionary(english VARCHAR(30), "
                "chinese VARCHAR(80), times SMALLINT, in_new_words SMALLINT)")
    close_db_connection(conn)

 

 

 

 

 

 

 

资料

https://www.jianshu.com/p/34afe4072598

https://www.cnblogs.com/0zcl/p/6504696.html