rawsql

% confuses python raw sql query

ε祈祈猫儿з 提交于 2019-12-01 19:18:27
问题 Following this SO question, I'm trying to "truncate" all tables related to a certain django application using the following raw sql commands in python: cursor.execute("set foreign_key_checks = 0") cursor.execute("select concat('truncate table ',table_schema,'.',table_name,';') as sql_stmt from information_schema.tables where table_schema = 'my_db' and table_type = 'base table' AND table_name LIKE 'some_prefix%'") for sql in [sql[0] for sql in cursor.fetchall()]: cursor.execute(sql) cursor

% confuses python raw sql query

倾然丶 夕夏残阳落幕 提交于 2019-12-01 18:09:19
Following this SO question , I'm trying to "truncate" all tables related to a certain django application using the following raw sql commands in python: cursor.execute("set foreign_key_checks = 0") cursor.execute("select concat('truncate table ',table_schema,'.',table_name,';') as sql_stmt from information_schema.tables where table_schema = 'my_db' and table_type = 'base table' AND table_name LIKE 'some_prefix%'") for sql in [sql[0] for sql in cursor.fetchall()]: cursor.execute(sql) cursor.execute("set foreign_key_checks = 1") Alas I receive the following error: C:\dev\my_project>my_script.py

Get related column on .annotate() data Django

荒凉一梦 提交于 2019-12-01 07:28:27
问题 I created this simple set of data to illustrate my point. It is a simple model with no further relations to any other model. I need to group the data above by topicid, find the max date for each group and then get the author for that date. info = TempModel.objects .values('topic') .annotate( max=Max('date')) .order_by('-max') Iterating through this in a template, <table> {% for item in info %} <tr> <td>{{ item.topicid }}</td> <td>{{ item.max }}</td> <td>{{ item.author }}</td> </tr> {% endfor

Execute raw SQL query in ASP.NET MVC, database first mode

馋奶兔 提交于 2019-12-01 05:52:16
The model of my project is database first, and uses remote access to database on another server. I need to use raw SQL query because my query is very complex and I feel more comfortable in SQl not LINQ. This is how I do: string query = "select * from Inquiry_TBL where ..."; using (educationEntities db = new educationEntities()) { var list = db.Database.SqlQuery<Inquiry_TBL>(query); ViewData["total"] = list.Count(); } The problem is sometimes I get the query result within a second, sometimes it just keep loading for a long time and gives me an error that 'Calling 'Read' when the data reader is

Execute raw SQL query in ASP.NET MVC, database first mode

空扰寡人 提交于 2019-12-01 02:11:21
问题 The model of my project is database first, and uses remote access to database on another server. I need to use raw SQL query because my query is very complex and I feel more comfortable in SQl not LINQ. This is how I do: string query = "select * from Inquiry_TBL where ..."; using (educationEntities db = new educationEntities()) { var list = db.Database.SqlQuery<Inquiry_TBL>(query); ViewData["total"] = list.Count(); } The problem is sometimes I get the query result within a second, sometimes

How to execute a raw update sql with dynamic binding in rails

我是研究僧i 提交于 2019-11-27 00:29:34
I want to execute one update raw sql like below: update table set f1=? where f2=? and f3=? This SQL will be executed by ActiveRecord::Base.connection.execute , but I don't know how to pass the dynamic parameter values into the method. Could someone give me any help on it? Brian Deterling It doesn't look like the Rails API exposes methods to do this generically. You could try accessing the underlying connection and using it's methods, e.g. for MySQL: st = ActiveRecord::Base.connection.raw_connection.prepare("update table set f1=? where f2=? and f3=?") st.execute(f1, f2, f3) st.close I'm not