Python数据库连接PostgreSQL (Module)错误



目前,我正在与Python学习数据库。

我试图使postgresql数据库连接下的OOP范式。我按照本文中的所有步骤进行了操作,但是在运行代码时出现了一个错误。

所有的代码都是一样的,我只是修改了database.ini文件与我的DB设置。

**database.inicode:

[postgresql]
host = localhost
port = 5432
database = dvdrental
user = postgres
password = 1234

**config.pycode:

# 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.pycode:

# 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.pycode:

# 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:ASUSMY CODES PYTHONIochordxsyydb_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:ASUSMY CODES PYTHONIochordxsyydb_connectionsrcdatadb_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:ASUSMY CODES PYTHONIochordxsyydb_connectionsrcdatadb_conn.py", line 9, in load_db_table
engine = psycopg2.connect(**params)
File "D:ASUSMY CODES PYTHONIochordxsyydb_connectionmain.py", line 9, in <module>
df = load_db_table(config_db = 'database.ini', query = 'SELECT * FROM actor LIMIT 5')

我已经检查了所有的代码是相同的文章,但我不知道为什么错误仍然发生。你有什么主意吗?

如果您有任何想法/解决方案,我将不胜感激。

谢谢。

信用:感谢上述文章的作者。

I think ">username";关键字database.ini必须是">user"。

** database.ini代码:
[postgresql]
host = localhost
port = 5432
database = dvdrental
user = postgres
password = 1234

[Solved]

大家好,我知道答案了。config.ini文件上有一个错误脚本。I change the script from "config_params[]" to "config_param[]"

** thecorrect脚本:

...
key = config_param[0]
value = config_param[1]
...

感谢大家的评论。

对齐this: config.py的缩进,如下所示。

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

相关内容

最新更新