django执行sql
http://docs.30c.org/djangobook2/chapter10/
def first_names(self, last_name):
cursor = connection.cursor()
cursor.execute("""
SELECT DISTINCT first_name
FROM people_person
WHERE last_name = %s""", [last_name])
return [row[0] for row in cursor.fetchone()]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #引擎
'NAME': 'coin_02', # 库名
'USER': 'root', # 用户名
'PASSWORD': '123456', # 密码
'HOST': '127.0.0.1', # 地址
'PORT': '3306', # 端口
},
'test':{
'ENGINE': 'django.db.backends.mysql', #引擎
'NAME': 'test', # 库名
'USER': 'root', # 用户名
'PASSWORD': '123456', # 密码
'HOST': '127.0.0.1', # 地址
'PORT': '3306', # 端口
},
官方文档;http://docs.30c.org/djangobook2/chapter05
from django.shortcuts import render_to_response
import MySQLdb
def book_list(request):
db = MySQLdb.connect(user='me', db='mydb', passwd='secret', host='localhost')
cursor = db.cursor()
cursor.execute('SELECT name FROM books ORDER BY name')
names = [row[0] for row in cursor.fetchall()]
db.close()
return render_to_response('book_list.html', {'names': names})
https://docs.djangoproject.com/en/2.0/topics/db/sql/
SQL语句 cursor.execute()使用占位符"%s",而不是直接在SQL中添加参数。如果您使用这种技
def GetList(request):
from .conn_mysql import transaction,connections
with connections['test'].cursor() as c:
c.execute("select * from duanzi WHERE id=%s;",[2])
# cursor.execute("select * from duanzi",[])
# cursor.execute("insert into duanzi(content,createtime) VALUES ('aa','1518186153')",[])
# transaction.commit_unless_managed()
row=c.fetchone()
# transaction.set_dirty()
# cursor.close()
return HttpResponse(row)
最新文章
- Ubuntu 部署 Node.js 应用
- OMXplayer播放视频的参数说明
- MongoDB副本集配置系列九:MongoDB 常见问题
- django 快速实现文件上传
- js 日期2015/12/22/16/45替换2015-12-22 16:45格式
- Linux 安装 nginx注意
- mysql定时脚本(event),类似oracle的job
- 3.5 linux 0.11 目标文件格式
- QTableWidget简单操作
- 如何修改select标签的默认下拉箭头样式?
- SPCircleView的使用(圆心向四周扩散动画)
- 流式计算与计算抽象化------《Designing Data-Intensive Applications》读书笔记15
- windows下注册表的操作
- H5键盘事件处理
- linux-grep-tail-find
- 微信小程序 - loading(组件)
- SVN无法Cleanup
- JDBC完成增加-修改-增加-查询
- Python的编码注释# -*- coding:utf-8 -*-
- 第一百七十六节,jQuery,插件
热门文章
- swarm 服务器安装
- Codeforces Round #326 Div.1 C.Duff in the Army 树上倍增
- [译]如何根据条件从pandas DataFrame中删除不需要的行?
- IP数据报格式 及分组转发算法
- mongo db 使用方法[转]
- hdu 3231 Box Relations (拓扑排序)
- SqlServer中临时表的应用
- 谈一谈深度学习之semantic Segmentation
- [NOI2003] 文本编辑器 (splay)
- saltshaker填坑