首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >python: postgreSQL using psycopg2 or psycopg

python: postgreSQL using psycopg2 or psycopg

作者头像
geovindu
发布2026-06-18 21:06:10
发布2026-06-18 21:06:10
700
举报

psycopg2 

代码语言:javascript
复制
# 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 

代码语言:javascript
复制
# 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()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档