通过py-manage shell操作数据库

进入app的同级目录下,执行python manage.py shell进入操作界面

from monitor import models    #导入app【monitor】的models

查看所建的表

>>> dir(models)
['Publisher', 'UserInfo', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'models']

查看表里面的内容,有两条记录aaa,BBB

>>> models.UserInfo.objects.all()
<QuerySet [<UserInfo: < aaa >>, <UserInfo: < BBB >>]>

查看表结构 

>>>print(models.UserInfo.objects.all().query)
SELECT "monitor_userinfo"."id", "monitor_userinfo"."Username", "monitor_userinfo"."Password", "monitor_userinfo"."Age" FROM "monitor_userinfo"

修改BBB内容

>>> k=models.UserInfo.objects.filter(Username='BBB').last()
>>> k.Age
22
>>> k.Age=100
>>> k.save()

添加,删除,更新,查看内容

>>> models.UserInfo.objects.create(Username='vip',Password='vip',Age=22)
<UserInfo: < vip >>

 >>> models.UserInfo.objects.filter(Username='vip').delete()
 (1, {'monitor.UserInfo': 1})


 >>> models.UserInfo.objects.filter(Username='aaa').update(Age='88')
 1

 >>> models.UserInfo.objects.filter(Username='aaa').values()
 <QuerySet [{'id': 35, 'Username': 'aaa', 'Password': 'aaa', 'Age': 88}]>

 

 

 

 

  

posted on 2018-06-08 15:43  世马  阅读(239)  评论(1编辑  收藏  举报