我想执行一个更新原始sql,如下所示:
update table set f1=? where f2=? and f3=?
Run Code Online (Sandbox Code Playgroud)
这个SQL将由执行ActiveRecord::Base.connection.execute,但我不知道如何将动态参数值传递给方法.
有人可以给我任何帮助吗?
我有这个光标
cursor.execute("SELECT price FROM Items WHERE itemID = (
SELECT item_id FROM Purchases
WHERE purchaseID = %d AND customer_id = %d)",
[self.purchaseID, self.customer])
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
'Cursor' object has no attribute '_last_executed'
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试这个:
cursor.execute("SELECT price FROM Items WHERE itemID = (
SELECT item_id FROM Purchases
WHERE purchaseID = 1 AND customer_id = 1)",
)
Run Code Online (Sandbox Code Playgroud)
没有错误.我该如何解决?
我正在从其他数据库迁移一些数据,所以我使用原始SQL查询将数据插入数据库.但我不知道如何从django中的原始sql查询中获取最后插入的id.我试过这个
affected_count1=cursor2.execute("table')")
and
SELECT IDENT_CURRENT(‘MyTable’)
Run Code Online (Sandbox Code Playgroud)
但它给了我错误"(1305,'FUNCTION pydev.SCOPE_IDENTITY不存在')"
那么请告诉我如何在django中的原始sq l查询中获取最后插入的id
我想使用DBContext SqlQuery执行原始sql,然后包含相关的entites.我尝试了以下但它没有加载相关的实体:
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();
Run Code Online (Sandbox Code Playgroud)
有帮助吗?
我的项目模型是数据库优先,并使用远程访问另一台服务器上的数据库.我需要使用原始SQL查询,因为我的查询非常复杂,我觉得在SQl而不是LINQ中感觉更舒服.
这是我的方式:
string query = "select * from Inquiry_TBL where ...";
using (educationEntities db = new educationEntities())
{
var list = db.Database.SqlQuery<Inquiry_TBL>(query);
ViewData["total"] = list.Count();
}
Run Code Online (Sandbox Code Playgroud)
问题是有时候我会在一秒钟内得到查询结果,有时它会长时间加载并给我一个错误,即当数据读取器关闭时"调用'读取'不是一个有效的操作."
这是为什么?我的代码有问题,还是因为我正在使用远程访问另一台服务器?切换到本地服务器会解决问题吗?
在这个问题之后,我试图在python中使用以下原始sql命令"截断"与某个django应用程序相关的所有表:
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")
Run Code Online (Sandbox Code Playgroud)
唉,我收到以下错误:
C:\dev\my_project>my_script.py
Traceback (most recent call last):
File "C:\dev\my_project\my_script.py", line 295, in <module>
cursor.execute(r"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%'")
File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 18, in …Run Code Online (Sandbox Code Playgroud)