sqlalchemy

Using SSL with SQLAlchemy

牧云@^-^@ 提交于 2021-02-06 15:18:30
问题 I've recently changed my project to use SQLAlchemy and my project runs fine, it used an external MySQL server. Now I'm trying to work with a different MySQL server with SSL CA, and it doesn't connect. (It did connect using MySQL Workbench, so the certificate should be fine) I'm using the following code: ssl_args = {'ssl': {'ca': ca_path}} engine = create_engine("mysql+pymysql://<user>:<pass>@<addr>/<schema>", connect_args=ssl_args) and I get the following error: Can't connect to MySQL server

Nonblocking Scrapy pipeline to database

允我心安 提交于 2021-02-06 11:56:14
问题 I have a web scraper in Scrapy that gets data items. I want to asynchronously insert them into a database as well. For example, I have a transaction that inserts some items into my db using SQLAlchemy Core: def process_item(self, item, spider): with self.connection.begin() as conn: conn.execute(insert(table1).values(item['part1']) conn.execute(insert(table2).values(item['part2']) I understand that it's possible to use SQLAlchemy Core asynchronously with Twisted with alchimia. The

SQLAlchemy through Paramiko SSH

耗尽温柔 提交于 2021-02-06 09:12:34
问题 I have a database on a server which I need to access through SSH. Right now I deal with the DB by using the command line to get the data. import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='XX.XX.XX', username='user', password='pass', port = YYY) query = "mysql -u " + username_sql + " -p" + password_sql +" dbb -e \"" + sql_query + "\"" ssh.exec_command(query.decode('string_escape')) ssh.close() Is there a way to do this

SQLAlchemy through Paramiko SSH

…衆ロ難τιáo~ 提交于 2021-02-06 09:07:01
问题 I have a database on a server which I need to access through SSH. Right now I deal with the DB by using the command line to get the data. import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='XX.XX.XX', username='user', password='pass', port = YYY) query = "mysql -u " + username_sql + " -p" + password_sql +" dbb -e \"" + sql_query + "\"" ssh.exec_command(query.decode('string_escape')) ssh.close() Is there a way to do this

SQLAlchemy through Paramiko SSH

一世执手 提交于 2021-02-06 09:06:52
问题 I have a database on a server which I need to access through SSH. Right now I deal with the DB by using the command line to get the data. import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='XX.XX.XX', username='user', password='pass', port = YYY) query = "mysql -u " + username_sql + " -p" + password_sql +" dbb -e \"" + sql_query + "\"" ssh.exec_command(query.decode('string_escape')) ssh.close() Is there a way to do this

What is the difference between Session and db.session in SQLAlchemy?

限于喜欢 提交于 2021-02-06 08:50:49
问题 In the event mapper level docs it says that Session.add() is not supported, but when I tried to do db.session.add(some_object) inside after_insert event it worked, example: def after_insert_listener(mapper, connection, user): global_group = Group.query.filter_by(groupname='global').first() a = Association(user,global_group) db.session.add(a) event.listen(User, 'after_insert', after_insert_listener) Basically any new user should be part of global_group, so I added it in the after_insert event.

What is the difference between Session and db.session in SQLAlchemy?

我的未来我决定 提交于 2021-02-06 08:50:28
问题 In the event mapper level docs it says that Session.add() is not supported, but when I tried to do db.session.add(some_object) inside after_insert event it worked, example: def after_insert_listener(mapper, connection, user): global_group = Group.query.filter_by(groupname='global').first() a = Association(user,global_group) db.session.add(a) event.listen(User, 'after_insert', after_insert_listener) Basically any new user should be part of global_group, so I added it in the after_insert event.

Return Pandas dataframe from PostgreSQL query with sqlalchemy

梦想与她 提交于 2021-02-05 12:50:52
问题 I want to query a PostgreSQL database and return the output as a Pandas dataframe. I created a connection to the database with 'SqlAlchemy': from sqlalchemy import create_engine engine = create_engine('postgresql://user@localhost:5432/mydb') I write a Pandas dataframe to a database table: i=pd.read_csv(path) i.to_sql('Stat_Table',engine,if_exists='replace') Based on the docs, looks like pd.read_sql_query() should accept a SQLAlchemy engine: a=pd.read_sql_query('select * from Stat_Table',con

Flask SQLAlchemy query with order_by returns error no such column

本小妞迷上赌 提交于 2021-02-05 12:09:50
问题 Here is my models: from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) db = SQLAlchemy(app) class Person(db.Model): __tablename__ = 'persons' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), nullable=False, unique=True) pets = db.relationship('Pet', backref='owner', lazy='dynamic') def __init__(self, *args, **kwargs): super(Person, self).__init__(*args, **kwargs) def __repr__(self): return f'<Person id:{self.id} name:{self.name}>'

PostgreSQL with SQLalchemy database is not found

巧了我就是萌 提交于 2021-02-05 11:31:05
问题 I am using the following code to create postgresql database using sqlalchemy: engine=create_engine('postgresql+psycopg2://postgres@localhost/testData') Base.metadata.create_all(engine) But it gives me the following error even though I manually created the database in psql: File "/home/ubuntu/venve/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 376, in connect return self.dbapi.connect(*cargs, **cparams) File "/home/ubuntu/venve/local/lib/python2.7/site-packages/psycopg2