DAL, Table, Field

□未翻訳

□翻訳中

■翻訳完了(細田謙二)

■レビュー(Omi Chiba)

DAL, Table, Field

DALのAPIを理解する最良の方法は、それぞれの関数を自分で試してみることです。これは、web2pyのシェルを介してインタラクティブに行うことができます。ただし、最終的には、DALコードはモデルとコントローラに行きます。

The best way to understand the DAL API is to try each function yourself. This can be done interactively via the web2py shell, although ultimately, DAL code goes in the models and controllers.

接続を作成してスタートしましょう。例なので、SQLiteを使用してもよいでしょう。この議論において、バックエンドのエンジンを変更しても何も変更することはありません。

Start by creating a connection. For the sake of example, you can use SQLite. Nothing in this discussion changes when you change the back-end engine.

1.

>>> db = DAL('sqlite://storage.db')

データベースはこれにより接続し、その接続はグローバル変数dbに格納されます。

The database is now connected and the connection is stored in the global variable db.

いつでも、接続文字列を取り出すことができます。

At any time you can retrieve the connection string.

1.

2.

>>> print db._uri

sqlite://storage.db

データベースの名前も取り出せます。

and the database name

1.

2.

>>> print db._dbname

sqlite

接続文字列は_uriと呼ばれます。Uniform Resource Identifierのインスタンスだからです。

The connection string is called a _uri because it is an instance of a Uniform Resource Identifier.

DALでは、同じデータベースや異なるデータベース、さらに、異なる種類のデータベースに対する複数の接続が可能です。ここでは、最も一般的な状況として、単一のデータベースを想定します。

The DAL allows multiple connections with the same database or with different databases, even databases of different types. For now, we will assume the presence of a single database since this is the most common situation.

DALの最も重要なメソッドはdefine_tableです:

The most important method of a DAL is define_table:

1.

>>> db.define_table('person', Field('name'))

これは、"name"フィールド(カラム)を持つ"person"というTableオブジェクトを定義し、格納し、返しています。このオブジェクトはまた、db.personに関連付けられているので、その戻り値を捉える必要はありません。

It defines, stores and returns a Table object called "person" containing a field (column) "name". This object can also be accessed via db.person, so you do not need to catch the return value.