I’m using pyodbc to query to an SQL Server database import datetime import pyodbc conn = pyodbc.connect(“Driver={SQL Server};Server=’dbserver’,Database=’db’, …
Tag: python
How come queries aren’t being added to Django’s db.connection.queries in tests?
I’m trying to capture the queries which my code submits to the database by examining the contents of django.db.connection.queries. For some reason though, after all the automatically produced setup queries are logged, no further queries are logged from my own code. The following test case demonstrates the behavior. And here are the results of running it: Would anyone be able
Aggregating save()s in Django?
I’m using Django with an sqlite backend, and write performance is a problem. I may graduate to a “proper” db at some stage, but for the moment I’m stuck with sqlite. I think that my write performance problems are probably related to the fact that I’m creating a large number of rows, and presumably each time I save() one it’s
Keep history in django to draw graphs
I am looking for the best way to have an history of my models (interger & float fields) in Django. I read Keeping a history of data changes in database and it seems that triggers are the best …
Python: Number of rows affected by cursor.execute(“SELECT …)
How can I access the number of rows affected by: Answer Try using fetchone: result will hold a tuple with one element, the value of COUNT(*). So to find the number of rows: Or, if you’d rather do it in one fell swoop: PS. It’s also good practice to use parametrized arguments whenever possible, because it can automatically quote arguments
Sqlalchemy complex in_ clause with tuple in list of tuples
I’m trying to find a way to cause SQLAlchemy to generate a query of the following form: select * from t where (a,b) in ((a1,b1),(a2,b2)); Is this possible? If not, any suggestions on a way to …
How to use variables in SQL statement in Python?
I have the following Python code: where var1 is an integer, var2 and var3 are strings. How can I write the variable names without Python including them as part of the query text? Answer Note that the parameters are passed as a tuple. The database API does proper escaping and quoting of variables. Be careful not to use the string
How to use “AND” in a Django filter?
How do I create an “AND” filter to retrieve objects in Django? e.g I would like to retrieve a row which has a combination of two words in a single field. For example the following SQL query does exactly that when I run it on mysql database: How do you accomplish this in Django using filters? Answer (update: this answer