执行原生SQL:


1 from django.db import connection, connections 2 cursor = connection.cursor() 3 cursor.execute( 4 """select * from tb where nid=%s""",[user.nid, ]) 5 result = cursor.fetchall()
赋值放在第二个参数,防止SQL注入
对于时间进行格式化:
- SQLite时间戳---->日期格式:
1 strftime("%Y-%m-01",ctime)
转换回时间戳:
strftime('%s',strftime("%Y-%m-01",ctime))
2.MySQ时间戳---->日期格式:
1 date_format(NOW(),'%Y-%m-%d %H:%i:%s')
MYSQL日期--->时间戳:
UNIX_TIMESTAMP('2006-11-04 12:23:00');
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
JS时间戳+new Date()
JS日期格式new Date()
执行原生SQL与时间格式转换
本文介绍如何在Django中执行原生SQL查询,包括使用Python变量防止SQL注入的方法,以及在SQLite和MySQL中将时间戳转换为日期格式和反之的技巧。
676

被折叠的 条评论
为什么被折叠?



