首页 > 技术文章 > Django的SQL注意事项(以及时间戳转换日期格式)

Neroi 2019-03-20 15:33 原文

执行原生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

 赋值放在第二个参数,防止SQL注入

对于时间进行格式化:

  1. 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()
 

 

推荐阅读