【发布时间】:2021-11-19 08:19:40
【问题描述】:
目前,我正在使用Python 学习数据库。
我正在尝试在 OOP 范式下建立 postgresql 数据库连接。
我按照article 中的所有步骤操作,但运行代码时出现错误。
所有代码都是一样的,我只是用我的数据库设置修改了database.ini 文件。
**database.ini代码:
[postgresql]
host = localhost
port = 5432
database = dvdrental
user = postgres
password = 1234
**config.py代码:
# import libraries
from configparser import ConfigParser
import configparser
from pathlib import Path
def get_project_root() -> Path:
''' Return project root folder '''
return Path(__file__).parents[1]
def config(config_db):
section = 'postgresql'
config_file_path = 'config/' + config_db
if (len(config_file_path) > 0 and len(section) > 0):
# create an instance of ConfigParser class
config_parser = ConfigParser()
# read the configuration file
config_parser.read(config_file_path)
# if the configuration file contains the provided section name
if(config_parser.has_section(section=section)):
# read options of the sections
config_params = config_parser.items(section=section)
# convert the list object to a python dictionary object
# define an empty dict
db_conn_dict = {}
# loop in the list
for config_param in config_params:
# get options key and value
key = config_params[0]
value = config_params[1]
# add the key value pair in the dictionary object
db_conn_dict[key] = value
# get connection object use above dictionary object
return db_conn_dict
**db_conn.py代码:
# import libraries
import pandas as pd
import psycopg2
from config.config import config
# take in a PostgreSQL table and outputs a pandas dataframe
def load_db_table(config_db, query):
params = config(config_db)
engine = psycopg2.connect(**params)
data = pd.read_sql(query, con = engine)
return data
**main.py代码:
# import library
from src.data.db_conn import load_db_table
from config.config import get_project_root
# project root
PROJECT_ROOT = get_project_root()
# read database
df = load_db_table(config_db = 'database.ini', query = 'SELECT * FROM actor LIMIT 5')
print(df)
问题是,当我运行程序时,我得到了error:
TypeError: connect() keywords must be strings
PS D:\ASUS\MY CODES PYTHON\Iochordxsyy\db_connection> python main.py
Traceback (most recent call last):
File "main.py", line 9, in <module>
df = load_db_table(config_db = 'database.ini', query = 'SELECT * FROM actor LIMIT 5')
File "D:\ASUS\MY CODES PYTHON\Iochordxsyy\db_connection\src\data\db_conn.py", line 9, in load_db_table
engine = psycopg2.connect(**params)
TypeError: connect() keywords must be strings
这是我debugged我的代码时的消息:
Exception has occurred: TypeError
connect() argument after ** must be a mapping, not NoneType
File "D:\ASUS\MY CODES PYTHON\Iochordxsyy\db_connection\src\data\db_conn.py", line 9, in load_db_table
engine = psycopg2.connect(**params)
File "D:\ASUS\MY CODES PYTHON\Iochordxsyy\db_connection\main.py", line 9, in <module>
df = load_db_table(config_db = 'database.ini', query = 'SELECT * FROM actor LIMIT 5')
我检查了所有代码都与文章相同,但我不知道为什么仍然会出现错误。你有什么想法吗?
如果您有任何想法/解决方案,我们将不胜感激。
谢谢。
信用: 感谢上述文章的作者。
【问题讨论】:
-
如果你打印
params,你会得到什么? edit 显示输出的问题。 -
嗨,@snakecharmerb 谢谢你的评论。我已经知道错误的来源。
config.ini有错误 --> "config_params[]"。它应该是“config_param[]”。谢谢你的cmets。非常感谢。
标签: python database postgresql psycopg2