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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值