自学内容网 自学内容网

python: postgreSQL using psycopg2 or psycopg

psycopg2 

# encoding: utf-8
# 版权所有 2024 ©涂聚文有限公司
# 許可資訊查看:言語成了邀功的功臣,還需要行爲每日來值班嗎?
# 描述: pip install --upgrade pip  PostgreSQL database adapter for Python
#  pip install psycopg2
# Author    : geovindu,Geovin Du 塗聚文.
# pip install pyqt6
# pip install pyside6
# pip install pyqt5
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# Datetime  : 2024/11/05 20:09
# User      : geovindu
# Product   : PyCharm
# Project   : PostgreSQL 9.6 PostgreSQL 9.6.24, compiled by Visual C++ build 1800, 64-bit
# File      : PostgreSQL.py
# explain   : 學習
 
import psycopg2
import sys
 
con = None
 
try:
 
    con = psycopg2.connect(database='TechnologyGame', user='postgres',
        password='888888',host='localhost', port='5432')
 
    cur = con.cursor()
    cur.execute('select * from School;')
    cur.fetchone()
    for record in cur:
            print(record)
             
    # 2
    #cur.execute('select version();')      
    #version = cur.fetchone()[0]
    #print(version)
 
except psycopg2.DatabaseError as e:
 
    print(f'Error {e}')
    sys.exit(1)
 
finally:
 
    if con:
        con.close()

psycopg 

# encoding: utf-8
# 版权所有 2024 ©涂聚文有限公司
# 許可資訊查看:言語成了邀功的功臣,還需要行爲每日來值班嗎?
# 描述: pip install --upgrade pip
#  pip install "psycopg[binary]"  https://github.com/psycopg/psycopg
# https://pypi.org/project/psycopg/
# Author    : geovindu,Geovin Du 塗聚文.
# pip install pyqt6
# pip install pyside6
# pip install pyqt5
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# Datetime  : 2024/11/05 20:09
# User      : geovindu
# Product   : PyCharm
# Project   : PostgreSQL 17.01 PostgreSQL 17.0 on x86_64-windows, compiled by msvc-19.41.34120, 64-bit
# File      : PostgreSQL.py
# explain   : 學習
 
import psycopg  # pip install "psycopg[binary]"  https://github.com/psycopg/psycopg
from psycopg import pq
from psycopg.errors import DatabaseError
import sys
import os
 
 
con = None
 
try:
 
    # Connect to an existing database "dbname=TechnologyGame user=postgres password=888888 host=localhost port=5433"
    with psycopg.connect(" host=localhost port=5433 user=postgres password=888888 dbname=TechnologyGame") as conn:
 
        # Open a cursor to perform database operations
        with conn.cursor() as cur:    
 
            # Query the database and obtain data as Python objects.
            cur.execute("SELECT * FROM School")
            cur.fetchone()   
 
       
            for record in cur:
                print(record)
                 
                 
            cur.execute('select version();')
            version = cur.fetchone()[0]
            print(version)
            # Make the changes to the database persistent
            #conn.commit()
 
except DatabaseError as e:
 
    print(f'Error {e}')
    sys.exit(1)
 
finally:
 
    if conn:
        conn.close()


原文地址:https://blog.csdn.net/geovindu/article/details/143750481

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!