What is the difference between executing raw SQL on the SQLAlchemy engine and the session? Specifically against a MSSQL database.
engine.execute('DELETE FROM MyTable WHERE MyId IN(1, 2, 3)')
versus
session.execute('DELETE FROM MyTable WHERE MyId IN(1, 2, 3)')
I've noticed that executing the SQL on the session, cause MSSQL to 'hang'.
Perhaps someone has an idea on how these two executions are different, or perhaps someone can point me where to further investigate.
The reason why MSSQL Server was hanging, was not because of the difference between calling execute on the engine or the session, but because a delete was being called on the table, without a commit, and then a subsequent read.
Related
I am using SQLAlchemy and pg8000 to connect to a Postgres database.
I have checked table pg_stat_activity, which shows me a few select queries in 'idle in transaction' state, many of those. But the application much more reads than writes, that is, inserts are few and far between.
I suspect that a new transaction is created for each query, even for simple select statements.
Is it possible to run a read-only query without the need for a transaction? So that it does not need to be committed/rolled back?
Currently, the app runs its queries with method sqlalchemy.engine.Engine.execute for CRUD operations and cursors for calling stored procedures. How should I update these method calls to indicate I want some of them not to start transactions?
I'm trying to connect to our internal Teradata database, using flask and sqlAlchemy along with a custom engine called sqlalchemy teradata. I put the database into the create_engine function likes so.
engine = sqlalchemy.create_engine('teradata://username:pw#server_name/database')
I've setup my dialect just like in the tests
registry.register("tdalchemy", "sqlalchemy_teradata.dialect", "TeradataDialect")
I'm getting a.
DatabaseError: (teradata.api.DatabaseError) (3807, u"[42S02] [Teradata][ODBC Teradata Driver][Teradata Database] Object 'table_name' does not exist
I can make raw sql queries just fine, I can also have alchemy do a query I construct and it pulls the data. I'm not sure what all is preventing things from working properly at all. When I test a similar call but looking at a database in an psql server it works just fine and pulls from that db without issue.
Also the pypi page says there is supposed to be an test/orm_test.py but it doesn't seem to have it.
I'd like to print the code a particular session generates and sends to the DB. I've seen this question but it echoes all the SQL that SQLAlchemy generates. I want just the SQL generated on behalf of a particular session. Bonus points if it can be printed before it is actually send to the DB (like we can with the Query object)
I know I can print the SQL of a query, but in this situation
a = Table(id=1)
session.add(a)
b = Table(id=4)
session.add(a)
b.column = 5
session.commit()
there is no query to print, but the session is beginning a transaction, inserting rows and committing the transaction.
I'd like the session to print to stdout (or a file, if that's not possible) the SQL commands it's sending to the DB.
The (immaginary) SQL I expect to find in the console is
BEGIN TRANSACTION
INSERT INTO Table (id) VALUES (1);
INSERT INTO Table (id, column) VALUES (4,5);
COMMIT TRANSACTION
I fully understand that the lines might not all come together and might be interleaved by other code prints (the session is beginning the transaction first, then later the code trigger the flush of the insert, and as last thing the session issues the commit)
Is that possible?
I'm writing a Python application that interacts with a MySql database. The basic way to do this is to connect to the database and execute the queries using the database's cursor object. Should I hardcode each query in the python code, or should I put each query in a stored procedure ? Which is the better design choice (keeping security and elegance in mind) ? Note that many of the queries are single-liners (select queries).
I'm trying to execute TSQL queries in a remote MSSQL database by using SQLAlchemy and pymssql. I've tested my procedural query directly in the database and it works as intended, it also works if I run it directly through pymssql. If I run a regular one liner queries such as:
select table_name from INFORMATION_SCHEMA.tables
Through SQLAlchemy this also works as it should. But when I try to execute the following TSQL query it does not actually create the table:
IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'SOME_TABLE')
BEGIN
CREATE TABLE SOME_TABLE (SOME_TEXT VARCHAR(255), SOME_TIME DATETIME)
END
it runs it as it was successful and if I try to read the result it from the execution it gives me "Resource already closed error" as expected since it is a CREATE query. However if I try to add data to table 'SOME_TABLE' it pukes at me and says that the table does not exist. Feels like it is only uploading the query as a function but never executes it. Any ideas? Or even better; TSQL queries that actually works when executing with SQLAlchemy and pymssql.
Thanks,
You need to commit your pending changes in the Session. For basic understanding of the process read Using the Session. Quick solution:
session.commit()
TIME and TEXT are reserve words.
I do not know how SQL Alchemy or pymmsql talks to SQL Server. Either native client or ODBC. It eventually all boils down to a tabular data stream (TDS) over a network protocol like TCP/IP.
Check out the reserve word list on TECHNET.
-- Create table ?
IF OBJECT_ID('DBO.SOMETABLE') IS NULL
CREATE TABLE DBO.SOMETABLE (MY_TEXT VARCHAR(255), MY_TIME DATETIME);
I use the OBJECT_ID function since it is less typing.
But NOT EXITS works with both SELECT FROM the sys.objects or information_schema.tables with correct WHERE clauses.