Python的ORM框架SQLObject入门实例
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)
四、查询记录
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是内容一致,这样做可以减少内存使用。可以在连接数据库时候设置参数,禁止该方式。
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'>]
你好