现在;我收到以下错误:
ProgrammingError: (psycopg2.ProgrammingError) relation "tableName" does not exist
LINE 2: FROM tableName
我认为是因为我没有在会话中使用该数据库;所以我去了procceded使用session.execute并使用sql语句USE来使用数据库,但它让我语法错误
engine = create_engine('postgresql://postgres:passwordhere@localhost:5432/test1')
sa.orm.configure_mappers() # IMPORTANT!
Base.metadata.create_all(engine)
session = sessionmaker(bind=engine)()
session.connection().connection.set_isolation_level(0)
session.execute('USE DATABASE test1;')
session.connection().connection.set_isolation_level(1)
我从上面的脚本中得到的错误:
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) syntax error at or near "USE"
LINE 1: USE DATABASE test1;
^
[SQL: 'USE DATABASE test1;'] (Background on this error at: http://sqlalche.me/e/f405)
正如人们所建议的那样;这是我的代码,在保存之前保存并检查数据库。
check_if_exists = session.query(latest_movies_scraper).filter_by(name = dictionary [' title'])。first()
if check_if_exists:
print check_if_exists.name
print 'skipping this...'
pass
else:
insert_to_db = latest_movies_scraper(name=dictionary['title'], url=dictionary['href'], image_url=dictionary['featured_image'])
session.add(insert_to_db)
session.commit()
class latest_movies_scraper(Base):
__tablename__ = 'latest_movies_scraper'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(255))
url = sa.Column(sa.Unicode(255))
image_url = sa.Column(sa.Unicode(255))
create = sa.Column(sa.DateTime, default=datetime.datetime.utcnow)
search_vector = sa.Column(TSVectorType('name'))
答案 0 :(得分:0)
mssql+pyodbc://servername/Databasename?driver=ODBC Driver 13 for SQL Server
eng = create_engine(mssql+pyodbc://servername/Databasename?driver=ODBC Driver 13 for SQL Server)
Session = sessionmaker(bind=eng, autoflush=false)
db = Session()
对于select使用db.query(“从表名中选择*”) 您还可以使用db.execute(“ select query”)