sqlobject mysql_SQLObject简明教程(一)

本文是SQLObject用于MySQL的简明教程,介绍了如何定义类、建立连接、创建数据库表,并展示了如何使用类来操作数据,包括插入、获取和更新记录。SQLObject提供了一个对象接口,使得数据库操作更加直观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

What's SQLObject?

SQLObject is a popular Object Relational Manager for providing an object interface to your database, with tables as classes, rows as instances, and columns as attributes.

I think the explanation is simple and enough, so let's start!

一.定义类

1.连接标识符:

scheme://[user[:password]@]host[:port]/database[?parameters]

其中scheme是sqlite, mysql, postgres, firebird, interbase, maxdb, sapdb, mssql, sybase其中之一;

示例:

mysql://user:passwd@host/database

mysql://host/database?debug=1

postgres://user@host/database?debug=&cache=

postgres://host:5432/database

sqlite:///full/path/to/database

sqlite:/C|/full/path/to/database

sqlite:/:memory:

可选的参数:

debug (默认值为False), debugOutput (默认值为False), cache (默认值为True), autoCommit (默认值为True), debugThreading (默认值为False).

如果你想传递一个True值到一个连接标识符中,那么只需传递一个非空字符串就可以了;空字符串则表示False值。

2.建立一个连接

1

4f1150b881333f12a311ae9ef34da474.png

db_filename

=

os.path.abspath(

'

data.db

'

)

2

4f1150b881333f12a311ae9ef34da474.png

if

os.path.exists(db_filename):

3

4f1150b881333f12a311ae9ef34da474.png    os.unlink(db_filename)

4

4f1150b881333f12a311ae9ef34da474.pngconnection_string

=

'

sqlite:

'

+

db_filename

5

4f1150b881333f12a311ae9ef34da474.pngconnection

=

connectionForURI(connection_string)

6

4f1150b881333f12a311ae9ef34da474.pngsqlhub.processConnection

=

connection3.生成一个简单的“地址薄”数据库,我们定义如下类:

1

4f1150b881333f12a311ae9ef34da474.pngclassPerson(SQLObject):24f1150b881333f12a311ae9ef34da474.png    firstName=StringCol()34f1150b881333f12a311ae9ef34da474.png    middleInitial=StringCol(length=1, default=None)44f1150b881333f12a311ae9ef34da474.png    lastName=StringCol()这个类对应的数据库Scheme如下:

4f1150b881333f12a311ae9ef34da474.pngCREATETABLEperson (

4f1150b881333f12a311ae9ef34da474.png    idINTPRIMARYKEYAUTO_INCREMENT,

4f1150b881333f12a311ae9ef34da474.png    first_nameTEXT,

4f1150b881333f12a311ae9ef34da474.png    middle_initialCHAR(1),

4f1150b881333f12a311ae9ef34da474.png    last_nameTEXT4f1150b881333f12a311ae9ef34da474.png);4.创建数据库表

很简单,就一句:

4f1150b881333f12a311ae9ef34da474.pngPerson.createTable()

5.更多

示例中firstName列为StringCol类型,当然你也可以换成其它类型。具体可参照:

http://www.sqlobject.org/SQLObject.html#column-types

你或许已经注意到了在类中并没有定义id列,它是隐式的。在MySQL中,它被定义成INT PRIMARY KEY AUTO_INCREMENT,在Postgres中是SERIAL PRIMARY KEY,而在SQLite中则是INTEGER PRIMARY KEY。你必须把这些值当作是不可变的。当然,你也可以覆盖“id”这个名字。

二.使用这个类

1.有了类,下面你需要做的就是创建一个新的对象(即新的一行),类实例化如下:

4f1150b881333f12a311ae9ef34da474.png>>>Person(firstName="John", lastName="Doe")

4f1150b881333f12a311ae9ef34da474.png2.你可以用get()方法取出已经存在的某行:

4f1150b881333f12a311ae9ef34da474.png>>>Person.get(1)

4f1150b881333f12a311ae9ef34da474.png3.这是一个略长些的例子:

4f1150b881333f12a311ae9ef34da474.png>>>p=Person.get(1)

4f1150b881333f12a311ae9ef34da474.png>>>p

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png>>>p.firstName

4f1150b881333f12a311ae9ef34da474.png'John'4f1150b881333f12a311ae9ef34da474.png>>>p.middleInitial='Q'4f1150b881333f12a311ae9ef34da474.png>>>p.middleInitial

4f1150b881333f12a311ae9ef34da474.png'Q'4f1150b881333f12a311ae9ef34da474.png>>>p2=Person.get(1)

4f1150b881333f12a311ae9ef34da474.png>>>p2

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png>>>pisp2

4f1150b881333f12a311ae9ef34da474.pngTrue

4.在这里,列被当作属性来访问。上述代码的“背后”又发生了什么呢?你可以在连接标识符中添加?debug=t,这样,在控制台中将打印出下面类似结果:

4f1150b881333f12a311ae9ef34da474.png>>>#This will make SQLObject print out the SQL it executes:4f1150b881333f12a311ae9ef34da474.png>>>Person._connection.debug=True

4f1150b881333f12a311ae9ef34da474.png>>>p=Person(firstName='Bob', lastName='Hope')

4f1150b881333f12a311ae9ef34da474.png1/QueryIns:  INSERT INTO person (last_name, middle_initial, first_name) VALUES ('Hope', 

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.pngNULL,'Bob')

4f1150b881333f12a311ae9ef34da474.png1/COMMIT  :  auto

4f1150b881333f12a311ae9ef34da474.png1/QueryOne:  SELECT first_name, middle_initial, last_name FROM person WHERE id=24f1150b881333f12a311ae9ef34da474.png1/COMMIT  :  auto

4f1150b881333f12a311ae9ef34da474.png>>>p

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png>>>p.middleInitial='Q'4f1150b881333f12a311ae9ef34da474.png1/Query   :  UPDATE person SET middle_initial='Q'WHERE id=24f1150b881333f12a311ae9ef34da474.png1/COMMIT  :  auto

4f1150b881333f12a311ae9ef34da474.png>>>p2=Person.get(1)

4f1150b881333f12a311ae9ef34da474.png>>>#Note: no database access, since we're just grabbing the same4f1150b881333f12a311ae9ef34da474.png>>>#instance we already had.上述代码,可以清晰地看出“后台”所做的事情。

5.作为一个小小的优化,你可以将独立地指定每个属性值,换成一次指定多个值,使用set方法:

4f1150b881333f12a311ae9ef34da474.pngp.set(firstName='Robert', lastName='Hope Jr.')

(未完待续)

参考资料:http://www.sqlobject.org/SQLObject.html

6a96a0c6001c2c59ca4d33664e986aaf.gif

欢迎大家访问我的个人网站 萌萌的IT人

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值