时间:2021-07-01 10:21:17 帮助过:370人阅读
一、安装
代码如下:
sudo pip install SQLObject
代码如下:
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
根据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()
代码如下:
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))
[]
你好