简体   繁体   English

SQLAlchemy无法使用绝对路径连接到本地数据库

[英]SQLAlchemy can't connect to a local database using an absolute path

After much hassle with getting the POSTGRESQL program configured with a user with a capital letter and figuring out how to write out an abs path in windows: set DATABASE_URL=postgresql:///c:\\\\Program Files\\\\PostgreSQL\\\\9.3\\\\data\\\\books.db 在为用用户用大写字母配置POSTGRESQL程序并弄清楚如何在Windows中写出abs路径而烦恼之后: set DATABASE_URL=postgresql:///c:\\\\Program Files\\\\PostgreSQL\\\\9.3\\\\data\\\\books.db

(no spaces ! I learned this the hardway !) (没有空格!我是很难学到的!)

in Windows I am still having difficulty connecting to the local databse I created with a test script I have. 在Windows中,我仍然很难连接到使用测试脚本创建的本地数据库。

within PSQL there exists a database called 'books' which shows up with '\\z'. 在PSQL中,存在一个名为“ books”的数据库,该数据库显示为“ \\ z”。 The default directory for saving databases has not been changed from POSTGRESQL default. 用于保存数据库的默认目录与POSTGRESQL的默认目录相同。 and yet when I run the program : 但是当我运行程序时:

import csv
import os

from sqlalchemy_utils import database_exists
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))


def import_csv():
    f = open("books.csv")
    reader = csv.reader(f)
    for isbn, title, author, year in reader:
        db.execute("INSERT INTO books (isbn, title, author, year) VALUES (:isbn, :title, :author, :year)",
                    {"isbn":isbn, "title":title, "author":author, "year":year})
    db.commit()

import_csv()

Why does this not connect ? 为什么不连接? ( I have cut out a lot of extraneous code here to show the active parts I am dealing with) (我在这里裁剪了很多无关的代码,以显示我正在处理的活动部分)

After much hassle with getting the POSTGRESQL program configured with a user with a capital letter and figuring out how to write out an abs path in windows: set DATABASE_URL=postgresql:///c:\\Program Files\\PostgreSQL\\9.3\\data\\books.db 在为使用用户用大写字母配置POSTGRESQL程序并弄清楚如何在Windows中写出abs路径而烦恼之后:设置DATABASE_URL = postgresql:/// c:\\ Program Files \\ PostgreSQL \\ 9.3 \\ data \\ books 。D b

Postgres is a database server. Postgres是一个数据库服务器。 You can't connect directly to one of the underlying files like that. 您不能直接连接到这样的基础文件之一。 You need to connect to the network service provided by the Postgres database server. 您需要连接到Postgres数据库服务器提供的网络服务。

The acceptable SQLAlchemy URLs for connecting to a Postgres database are shown here . 此处显示用于连接到Postgres数据库的可接受的SQLAlchemy URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM