Posted in Python onApril 28, 2014
SQLObject和SQLAlchemy都是Python语言下的ORM(对象关系映射)解决方案,其中SQLAlchemy被认为是Python下事实上的ORM标准。当然,两者都很优秀。
一、安装
sudo pip install SQLObject
使用SQLObject操作mysql时候报错ImportError: No module named MySQLdb,那便安装MySQLdb:
sudo pip install MySQL-python
没想到又报错了:
_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。
#-*-encoding: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中每个用户的编写的文章:
class User(SQLObject): 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'>] 你好
Python的ORM框架SQLObject入门实例
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@