前提: model文件已经创建对应表结构 在Django项目启动的时候,会根据在setting的配置连接数据库。返回的数据类型是tuple或者tuple_List 一.查询数据
方法一:通过object.raw直接通过SQL语句查询
data = BaspRole.objects.raw('select * from basp_role where status=1 order by id desc limit 2')
# sql = 'select * from basp_resource where status=1 order by id desc;'
# resource_list = BaspResource.objects.raw(sql)
方法二:使用游标查询
cursor = connection.cursor()
#查询
cursor.execute("select * from user")
#返回一行
raw = cursor.fetchone()
#返回所有
results = cursor.fetchall()
方法三: 通过变量来查询数据
data= BaspSys.objects.raw('select * from basp_sys where system_id =" %s"' %system_id)
方法四: 不直接更新, 往后加某些字符, 使用concat函数
BaspRole.objects.filter(role_id=role_id).update(remark=Concat('remark', Value('已共享给系统')))
二.更新数据,使用filter
BaspGroup.objects.filter(group_id=group_id).update(status=0)
三.插入新数据
datagroup = BaspGroup(system_code=system_code, remark=remark, status=1)
datagroup.save()