9.16 ORM
阿新 • • 發佈:2018-10-21
sign 9.1 分享圖片 lan class pda char HERE pri
ORM只能做到表這一層,不能創建數據庫。
#sql中的表 #創建表: CREATE TABLE employee( id INT PRIMARY KEY auto_increment , name VARCHAR (20), gender BIT default 1, birthday DATA , department VARCHAR (20), salary DECIMAL (8,2) unsigned, ); #sql中的表紀錄 #添加一條表紀錄: INSERT employee (name,gender,birthday,salary,department) VALUES ("alex",1,"1985-12-12",8000,"保潔部"); #查詢一條表紀錄: SELECT * FROM employee WHERE age=24; #更新一條表紀錄: UPDATE employee SET birthday="1989-10-24" WHERE id=1; #刪除一條表紀錄: DELETE FROM employee WHERE name="alex" #python的類 class Employee(models.Model): id=models.AutoField(primary_key=True) name=models.CharField(max_length=32) gender=models.BooleanField() birthday=models.DateField() department=models.CharField(max_length=32) salary=models.DecimalField(max_digits=8,decimal_places=2) #python的類對象 #添加一條表紀錄: emp=Employee(name="alex",gender=True,birthday="1985-12-12",epartment="保潔部") emp.save() #查詢一條表紀錄: Employee.objects.filter(age=24) #更新一條表紀錄: Employee.objects.filter(id=1).update(birthday="1989-10-24") #刪除一條表紀錄: Employee.objects.filter(name="alex").delete()
9.16 ORM