分类: Python/Ruby
2013-09-11 17:02:39
4.数据库表的创建 数据插入 管理
4.1 创建表
root@ubuntu:/opt/serManager/sshCMD# vim models.py
from django.db import models
class SerInfo(models.Model):
gid = models.IntegerField()
user = models.CharField(max_length=30)
password = models.CharField(max_length=30)
ser_name = models.CharField(max_length=30)
ip = models.CharField(max_length=30)
key = models.BooleanField()
cmd = models.CharField(max_length=60)
email = models.EmailField(max_length=75)
status = models.BooleanField()
datetime = models.DateField()
4.2 检查是否有语法错误
root@ubuntu:/opt/serManager# python manage.py validate
0 errors found 表示没有语法错误
root@ubuntu:/opt/serManager#
4.3 查看数据库建表过程
*sshCMD 是应用的名称
root@ubuntu:/opt/serManager# python manage.py sqlall sshCMD
BEGIN;
CREATE TABLE `sshCMD_serinfo` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`gid` integer NOT NULL,
`user` varchar(30) NOT NULL,
`password` varchar(30) NOT NULL,
`ser_name` varchar(30) NOT NULL,
`ip` varchar(30) NOT NULL,
`key` bool NOT NULL,
`cmd` varchar(60) NOT NULL,
`email` varchar(75) NOT NULL,
`status` bool NOT NULL,
`datetime` date NOT NULL
)
;
COMMIT;
root@ubuntu:/opt/serManager#
4.4 经过上面的检验数据无误,将数据写入数据库
root@ubuntu:/opt/serManager# python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table sshCMD_serinfo
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'root'):
Email address:
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
root@ubuntu:/opt/serManager#
4.5 查看数据库
root@ubuntu:/opt/serManager# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| csvt |
| csvt04 |
| djangodb |
| hans01 |
| mysql |
| performance_schema |
| pylogs |
| python |
| serManager |
| test |
| todo |
| triweb |
| weblog |
+--------------------+
14 rows in set (0.14 sec)
mysql> use serManager;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_serManager |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_content_type |
| django_session |
| django_site |
| sshCMD_serinfo |
+----------------------------+
10 rows in set (0.00 sec)
mysql>
下面是可参考内容:
#############################
向表中插入数据
root@ubuntu:/opt/triWeb# python manage.py shell
In [9]: p1=Publisher(name='Apress',address='2855 Telegraph Avenue',city='Berkeley',state_province='CA',country='U.S.A',website='')
In [10]: p1.save()
查看数据
In [11]: p_list=Publisher.objects.all()
In [12]: p_list
Out[12]: [
In [13]:
过滤数据,查出city为jilin的所有数据
p=Publisher.objects.filter(city='jilin')
修改数据,相当于update,修改city=jilin的数据为city=changchun
p=Publisher.objects.get(city='jilin')
p.city='changchun'
p.save()