I'm running a PostgreSQL database on a server and I'm trying to connect to it using SQLAlchemy. I found that sqlacodegen was a good tool to generate the MetaData object automatically along with its Tables. But when I try and run sqlacodgen postgresql+psycopg2://username:password#host:5432/dbname, I only get this:
# coding: utf-8
from sqlalchemy import MetaData
metadata = MetaData()
The connection string is definitely correct, and the database is up - I ran a small Python script using that connection string to connect to the database and used execute to run a query on it, and it returns exactly what I expected.
I'm not sure where even to start with debugging this. What could I do to see what's wrong? Is there some sort of requirement that sqlacodegen has that I'm missing?
As it turns out this problem was unrelated to sqlacodegen. All the tables in my database are prefixed with dbname. Passing in --schema dbname works.
Related
Assume that I have two Postgresql servers running, On server-1 I have a table called "employee_info", I want to use this table in python flask code through SQL alchemy, right now I have connected to "server-2" from my application.
How can use the "employee_info" from server-1 in my current code?
You might want to try using Multiple Databases with Binds (from the flask-sqlalchemy documentation).
I'm getting the next error while trying to run my Flask app:
sqlalchemy.exc.OperationalError
OperationalError: (_mysql_exceptions.OperationalError) (1049, "Unknown database '/home/gerardo/Documentos/python_web_dev/flask-intro/app2.db'")
so it seems like there is no database.. but I ran the next scripts using sqlalchemy_utils and everything was ok:
engine = create_engine("mysql://root:#localhost/home/gerardo/Documentos/python_web_dev/flask-intro/app2.db")
create_database(engine.url)
but still I get the error..
You have confused MySQL, a database server, with SQLite, a database in a file. You created a SQLite file, but are trying to tell MySQL to connect to it, which makes no sense.
Use the sqlite dialect in the connection string.
You can avoid typing the whole path (and tying the app to that path) by pointing to the file relative to the app's location.
import os
db_path = os.path.realpath(os.path.join(os.path.dirname(__file__), 'app2.db'))
engine = create_engine('sqlite://{}'.format(db_path))
Consider using Flask-SQLAlchemy rather than trying to manage the database yourself.
I use Mysql5.6, Mysqldb_1.2.4, python_2.7.3 with win7 system. As the title says, I've modified Mysql's configure file my.ini by changing charset.
After restarting mysql service, I use Mysql commandline:
When I create a database in mysql workbench, i use this collation--utf8mb4_unicode_ci:
However, I get this:
I really really wanna know how to create a utf8mb4 database, because I need the fix emoj characters inserted into database problem, which I think can be solved if I can create a utf8mb4 database.
I just fixed this problem, by executing("SET NAMES utf8mb4") after creating connection with your database and acquiring cursor object.
I'm trying to connect to a Sybase database in Python (using the python-sybase DBAPI and sqlalchemy module), and I'm currently receiving the following error:
ct_connect(): directory service layer: internal directory control layer error: There was an error encountered while binding to the directory service
Here's the code:
import sqlalchemy
connect_url = sqlalchemy.engine.url.URL(drivername='pysybase', username='read_only', password='*****', host='hostname', port=9000, database='tablename', query=None)
db = sqlalchemy.create_engine(connect_url)
connection = db.connect()
I've also tried to connect without sqlalchemy - ie, just importing the Python Sybase module directly and attempting to connect, but I still get the same error.
I've done quite a bit of googling and searching here on SO and at the doc sites for each of the packages I'm using. One common suggestion was to verify the DSN settings, as that's what's causing ct_connect() to trip up, but I am able to connect to and view the database in my locally-installed copy of DBArtisan just fine, and I believe that uses the same DSN.
Perhaps I should attempt to connect in a way without a DSN? Or is there something else I'm missing here?
Any ideas or feedback are much appreciated, thank you!
I figured out the issue for anyone else who might be having a similar problem.
Apparently, even though I had valid entries for the hostname in my sql.ini file and DSN table, Sybase was not reading it correctly - I had to open DSEdit (one of the tools that comes with Sybase) and re-enter the server/hostname info.
How do you unit test your python DAL that is using postgresql.
In sqlite you could create in-memory database for every test but this cannot be done for postgresql.
I want a library that could be used to setup a database and clean it once the test is done.
I am using Sqlalchemy as my ORM.
pg_tmp(1) is a utility intended to make this task easy. Here is how you might start up a new connection with SQLAlchemy:
from subprocess import check_output
from sqlalchemy import create_engine
url = check_output(['pg_tmp', '-t'])
engine = create_engine(url)
This will spin up a new database that is automatically destroyed in 60 seconds. If a connection is open pg_tmp will wait until all active connections are closed.
Have you tried testing.postgresql?
You can use nose to write your tests, then just use SQLAlchemy to create and clean the test database in your setup/teardown methods.
There's QuickPiggy too, which is capable of cleaning up after itself.
From the docs:
A makeshift PostgresSQL instance can be obtained quite easily:
pig = quickpiggy.Piggy(volatile=True, create_db='somedb')
conn = psycopg2.connect(pig.dsnstring())