rawsql

Python: Create Sql raw query with In clause with list data

一曲冷凌霜 提交于 2021-01-28 10:14:07
问题 Recently I stuck for a moment while preparing the raw Sql Query having In clause to it and the In clause data is a python list . Okay Let me put my example here. Sql Query that I wanted sql_query = 'SELECT * FROM student WHERE first_name IN ("Dean");' From the data I was having data = ["Dean"] query = 'SELECT * FROM student WHERE first_name IN %s;' % str(tuple(data)) # result was "SELECT * FROM student WHERE first_name IN ('Dean',) " # see the *Comma* just before close parentheses which is a

Running Plain SQL dynamically in Quill using infix fails with wrong query syntax during runtime

与世无争的帅哥 提交于 2020-03-05 00:32:40
问题 I want to construct my query in plain SQL and then run it using Quill, I am using infix operator . My code is like this. case class Employee(name: String, age: String, company_name: String) case class Company(name: String, pin_code: String) case class CombinedEmployee(employee_age: Int, employee_name: Option[String], company_name: String, pin: Option[String]) val sql = "SELECT t1.age AS employee_age, t1.name AS employee_name, t2.name AS company_name, t2.pin as pin FROM employee t1 JOIN

Running Plain SQL dynamically in Quill using infix fails with wrong query syntax during runtime

孤街醉人 提交于 2020-03-05 00:22:37
问题 I want to construct my query in plain SQL and then run it using Quill, I am using infix operator . My code is like this. case class Employee(name: String, age: String, company_name: String) case class Company(name: String, pin_code: String) case class CombinedEmployee(employee_age: Int, employee_name: Option[String], company_name: String, pin: Option[String]) val sql = "SELECT t1.age AS employee_age, t1.name AS employee_name, t2.name AS company_name, t2.pin as pin FROM employee t1 JOIN

Rewrite raw SQL as Django query

≯℡__Kan透↙ 提交于 2020-01-02 11:16:11
问题 I am trying to write this raw SQL query, info_model = list(InfoModel.objects.raw('SELECT *, max(date), count(postid) AS freq, count(DISTINCT author) AS contributors FROM crudapp_infomodel GROUP BY topicid ORDER BY date DESC')) as a django query. The following attempt does not work as I can't get related fields for 'author' and 'post'. info_model = InfoModel.objects.values('topic') .annotate( max=Max('date'), freq=Count('postid'), contributors=Count('author', distinct=True)) .order_by('-max')

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

别说谁变了你拦得住时间么 提交于 2019-12-17 06:31:43
问题 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? 回答1: 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

Error executing MySQL query via ebean using RawSql

喜你入骨 提交于 2019-12-11 05:52:51
问题 In a Play! 2.x application, I'm trying to send a simple query to a MySQL server using ebean. My complete class looks as follows: public static List<Venue> search(String query) { List<Venue> matches = new ArrayList<Venue>(); try { String q = query.replace(" ", ""); String sql = "SELECT v.id, c.company, c.postcode \n" + "FROM venue v \n" + "JOIN contact c ON (c.id = v.id) \n" + "WHERE REPLACE(c.postcode, ' ', '') LIKE '%" + q + "%' \n" + " OR c.company LIKE '%" + q + "%'"; RawSql rawSql =

How to set an entity field that does not exist on the table but does exists in the raw SQL as an alias?

ε祈祈猫儿з 提交于 2019-12-11 03:48:41
问题 Lets say that we have a query like this one: SELECT *, (CUSTOM_EXPRESSION) as virtualfield FROM users The user's entity itself has the "virtualfield" but the mapping annotations no, since the table doesn't have this field. Assuming that it is executed as a raw SQL, how do we populate the entity with the field above? 回答1: I've found the answer. To do so, you need to use a scalar value. For instance: $rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->getEntityManager()); $rsm-

Error: Cursor' object has no attribute '_last_executed

寵の児 提交于 2019-12-10 12:36:48
问题 I have this cursor cursor.execute("SELECT price FROM Items WHERE itemID = ( SELECT item_id FROM Purchases WHERE purchaseID = %d AND customer_id = %d)", [self.purchaseID, self.customer]) I get this error 'Cursor' object has no attribute '_last_executed' But when I try this: cursor.execute("SELECT price FROM Items WHERE itemID = ( SELECT item_id FROM Purchases WHERE purchaseID = 1 AND customer_id = 1)", ) there is no error. How do I fix this? 回答1: I encountered this problem too. I changed the

Rewrite raw SQL as Django query

大兔子大兔子 提交于 2019-12-06 09:44:01
I am trying to write this raw SQL query, info_model = list(InfoModel.objects.raw('SELECT *, max(date), count(postid) AS freq, count(DISTINCT author) AS contributors FROM crudapp_infomodel GROUP BY topicid ORDER BY date DESC')) as a django query. The following attempt does not work as I can't get related fields for 'author' and 'post'. info_model = InfoModel.objects.values('topic') .annotate( max=Max('date'), freq=Count('postid'), contributors=Count('author', distinct=True)) .order_by('-max') With raw SQL I can use SELECT * but how can I do the equivalent with the Django query? The model is,

EF 4.1 - DBContext SqlQuery and Include

时间秒杀一切 提交于 2019-12-04 03:20:00
问题 I want to execute a raw sql using DBContext SqlQuery and then include related entites. I've tried the following but it doesn't load the related entities: string sql = "Select * from client where id in (select id from activeclient)"; var list = DbContext.Database.SqlQuery<Client>(sql).AsQueryable().Include(c => c.Address).Include(c => c.Contactinfo).ToList(); Any help? 回答1: It is not possible. Include works only with ESQL or linq-to-entities because it must be processed during query building