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 syscon = Nonetry: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 oscon = Nonetry:# 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 operationswith 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()