0
Good morning, use Peewee with Postgresql, working very well.
The connection credentials are as follows::
db = PostgresqlDatabase (
'ALTERDATA_WSHOP', # Required by Peewee.
user = 'postgres', # Will be passed directly to psycopg2.
password = '# abc123 #', # Ditto.
host = 'localhost') # Ditto.
How do I save this to a. py file and recover this information if any?
Below my code:
from datetime import datetime
from peewee import (PostgresqlDatabase, CompositeKey, Model, CharField, DateTimeField,
DoubleField, IntegerField, TextField, BooleanField)
def open_config_pg():
try:
with open('pg_config.py', 'r') as f:
# Conteúdo do arquivo
db = f.read()
except:
text = "PostgresqlDatabase('ALTERDATA_WSHOP', user='postgres', password='#abc123#', host='localhost')"
with open('pg_config.py', 'w') as f:
# Conteúdo do arquivo
f.write(text)
with open('pg_config.py', 'r') as f:
# Conteúdo do arquivo
db = f.read()
return db
class UnknownField(object):
def __init__(self, *_, **__): pass
class BaseModel(Model):
class Meta:
db = open_config_pg()
database = db
My return is this:
"/home/Elias/. pyenv/versions/3.8.3/lib/python3.8/site-Packages/Peewee.py", line 2150, at _execute cursor = database.execute (self) Attributeerror: object 'str' has no 'run' attribute'
Return is out of function.
– nmenezes
It is true! But unfortunately it returned another error: raise Valueerror('Query has not been executed.') Valueerror: Query has not been executed.
– Elias Coutinho
See that what you are reading from the file is the text of a program. Simply reading the text and returning does not make it run, it is just a string pro Python. Actually, you should write the connection parameters into the file and call the code with them. An easy solution is to use json
– nmenezes