Python的ORM框架SQLObject入门实例
sqlobject和sqlalchemy都是python语言下的orm(对象关系映射)解决方案,其中sqlalchemy被认为是python下事实上的orm标准。当然,两者都很优秀。
一、安装
使用sqlobject操作mysql时候报错importerror: no module named mysqldb,那便安装mysqldb:
没想到又报错了:
_mysql.c:29:20: fatal error: python.h: no such file or directory
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
解决方法:
sudo apt-get install libmysqlclient-dev python-dev
二、使用其创建表
将mysql默认存在的test数据库的编码改为utf-8。
from sqlobject import *
uri = r'mysql://root:passwd@127.0.0.1/test?charset=utf8'
sqlhub.processconnection = connectionforuri(uri)
class user(sqlobject):
name = stringcol(length=10, notnone=true)
email = stringcol(length=20, notnone=true)
password = stringcol(length=20, notnone=true)
user.createtable()
运行后,会看到test数据库下出现表user,我们使用show create table user;查看user表的创建语句,结果如下:
create table `user` (
`id` int(11) not null auto_increment,
`name` varchar(10) not null,
`email` varchar(20) not null,
`password` varchar(20) not null,
primary key (`id`)
) engine=innodb default charset=utf8
三、添加/删除记录
现在我们尝试着添加和删除记录。
user1 = user(name='user1',email='user1@163.com',password='111')
user2 = user(name='user2',email='user2@163.com',password='222')
运行后,使用select * from user能看到这两个记录:
mysql> select * from user;
+----+-------+---------------+----------+
| id | name | email | password |
+----+-------+---------------+----------+
| 1 | user1 | user1@163.com | 111 |
| 2 | user2 | user2@163.com | 222 |
+----+-------+---------------+----------+
2 rows in set (0.00 sec)
删除数据
u2 = user.get(2)
print user.delete(u2.id)
四、查询记录
通过id获取数据:
u1 = user.get(1)
u1_1 = user.get(1)
u2 = user.get(2)
print id(u1), u1
print id(u1_1), u1_1
print id(u2), u2
输出结果:
23864656
23864656
23930512
由于id(u1)和id(u1_1)是相等的,所以u1和u1_1是内容一致,这样做可以减少内存使用。可以在连接数据库时候设置参数,禁止该方式。
根据name进行查询:
users = user.select(user.q.name=="user1")
print users
print list(users)
输出结果:
select user.id, user.name, user.email, user.password from user where ((user.name) = ('user1'))
[]
模糊查询:
users = user.select(user.q.name.startswith('u'))
print users
print list(users)
users = user.select(user.q.name.contains('ser1'))
print users
print list(users)
运行结果:
select user.id, user.name, user.email, user.password from user where (user.name like ('u%') escape '\\')
[, ]
select user.id, user.name, user.email, user.password from user where (user.name like ('%ser1%') escape '\\')
[]
一对多映射
我们新建一个表,保存user中每个用户的编写的文章:
name = stringcol(length=10, notnone=true)
email = stringcol(length=20, notnone=true)
password = stringcol(length=20, notnone=true)
class article(sqlobject):
title = stringcol(length=100, notnone=true)
content = stringcol(notnone=true)
user = foreignkey('user')
article.createtable()
运行后,使用show create table article查看创建语句:
create table `article` (
`id` int(11) not null auto_increment,
`title` varchar(100) not null,
`content` text not null,
`user_id` int(11) default null,
primary key (`id`),
key `article_user_id_exists` (`user_id`),
constraint `article_user_id_exists` foreign key (`user_id`) references `user` (`id`)
) engine=innodb default charset=utf8
添加数据:
u1 = user.get(1)
a1 = article(title='title1',content='你好',user=u1)
查询数据:
u1 = user.get(1)
a1 = article.select(article.q.user == u1)
print a1
print list(a1)
print list(a1)[0].content
这种方式也可以:
a1 = article.select(article.q.userid == 1)
print a1
print list(a1)
print list(a1)[0].content
运行结果:
select article.id, article.title, article.content, article.user_id from article where ((article.user_id) = (1))
[<article title='title1'>]
你好