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.
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 have the following scenario: users can log in and update certain tables, and whenever that table is updated I would like to call a Python script that will query the same database for additional information (joins) and then use Django's email system to email all users in that group (same role) that information.
My current methodology is to create a user-defined function in SQLite3, and then create a trigger that will call that function (which is linked to a Python file). Is this possible with SQLite?
Django app, SQLite and Python.
import sqlite3
def email_materials_submittal(project_id):
print(project_id)
con = sqlite3.connect('../db.sqlite3')
con.create_function("FUNC_EMS", 1, email_materials_submittal)
con.commit()
cur = con.cursor()
cur.execute("CREATE TRIGGER TRIG_EMS AFTER INSERT ON core_level BEGIN SELECT FUNC_EMS(NEW.project_id); END;")
con.commit()
con.close()
I want SQL to call an external Python script whenever a new row is inserted into a certain table. Tried the code above, but SQLite throws the error: no such function: FUNC_EMS
This task is much easier using signals for some notifications and a combination of custom management commands and crontab jobs for others (date reminders). Django is awesome!
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.
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 insert all values of a list to my sqlite3 database. When I simulate this query by using the python interactive interpreter, I am able to insert the single value to DB properly. But my code fails while using an iteration:
...
connection=lite.connect(db_name)
cursor=connection.cursor()
for name in match:
cursor.execute("""INSERT INTO video_dizi(name) VALUES (?)""",(name,))
connection.commit()
...
error:cursor.execute("""INSERT INTO video_dizi(name) VALUES (?)""",(name,))
sqlite3.OperationalError: database is locked
Any way to overcome this problem?
Do you have another connection elsewhere in your code that you use to begin a transaction that is still active (not committed) when you try to commit the operation that fails?
As this error can happen because you have opened your site.db or database file in DBbrowser type application to view in interactive database interface. Just close that it will work fine.
Because your database is use by another process or connection. If you need real concurrency, use a real RDBMS.