Make examples in quickstart all work.
When creating the table in the 'Storing data' section the table created was called 'person', rather than 'user' making the other examples not work because 'user' was referred to rather than 'person'. I added countries to the entered data so that later queries in the quickstart return some data. I changed examples of returned data in 'Inspecting databases and tables' match what is actually returned. In 'Reading data from tables' I make the examples return real data.
This commit is contained in:
parent
e7d317b38b
commit
4caa2cb0cb
@ -41,18 +41,18 @@ To store some data you need to get a reference to a table. You don't need
|
|||||||
to worry about whether the table already exists or not, since dataset
|
to worry about whether the table already exists or not, since dataset
|
||||||
will create it automatically::
|
will create it automatically::
|
||||||
|
|
||||||
# get a reference to the table 'person'
|
# get a reference to the table 'user'
|
||||||
table = db['person']
|
table = db['user']
|
||||||
|
|
||||||
Now storing data in a table is a matter of a single function call. Just
|
Now storing data in a table is a matter of a single function call. Just
|
||||||
pass a `dict`_ to *insert*. Note that you don't need to create the columns
|
pass a `dict`_ to *insert*. Note that you don't need to create the columns
|
||||||
*name* and *age* – dataset will do this automatically::
|
*name* and *age* – dataset will do this automatically::
|
||||||
|
|
||||||
# Insert a new record.
|
# Insert a new record.
|
||||||
table.insert(dict(name='John Doe', age=46))
|
table.insert(dict(name='John Doe', age=46, country='China'))
|
||||||
|
|
||||||
# dataset will create "missing" columns any time you insert a dict with an unknown key
|
# dataset will create "missing" columns any time you insert a dict with an unknown key
|
||||||
table.insert(dict(name='Jane Doe', age=37, gender='female'))
|
table.insert(dict(name='Jane Doe', age=37, country='France', gender='female'))
|
||||||
|
|
||||||
.. _dict: http://docs.python.org/2/library/stdtypes.html#dict
|
.. _dict: http://docs.python.org/2/library/stdtypes.html#dict
|
||||||
|
|
||||||
@ -72,17 +72,17 @@ first. To start exploring, let's find out what tables are stored in the
|
|||||||
database:
|
database:
|
||||||
|
|
||||||
>>> print(db.tables)
|
>>> print(db.tables)
|
||||||
set([u'user', u'action'])
|
[u'user']
|
||||||
|
|
||||||
Now, let's list all columns available in the table ``user``:
|
Now, let's list all columns available in the table ``user``:
|
||||||
|
|
||||||
>>> print(db['user'].columns)
|
>>> print(db['user'].columns)
|
||||||
[u'id', u'name', u'email', u'pwd', u'country']
|
[u'id', u'country', u'age', u'name', u'gender']
|
||||||
|
|
||||||
Using ``len()`` we can get the total number of rows in a table:
|
Using ``len()`` we can get the total number of rows in a table:
|
||||||
|
|
||||||
>>> print(len(db['user']))
|
>>> print(len(db['user']))
|
||||||
187
|
2
|
||||||
|
|
||||||
Reading data from tables
|
Reading data from tables
|
||||||
------------------------
|
------------------------
|
||||||
@ -94,13 +94,13 @@ Now let's get some real data out of the table::
|
|||||||
If we simply want to iterate over all rows in a table, we can omit :py:meth:`all() <dataset.Table.all>`::
|
If we simply want to iterate over all rows in a table, we can omit :py:meth:`all() <dataset.Table.all>`::
|
||||||
|
|
||||||
for user in db['user']:
|
for user in db['user']:
|
||||||
print(user['email'])
|
print(user['age'])
|
||||||
|
|
||||||
We can search for specific entries using :py:meth:`find() <dataset.Table.find>` and
|
We can search for specific entries using :py:meth:`find() <dataset.Table.find>` and
|
||||||
:py:meth:`find_one() <dataset.Table.find_one>`::
|
:py:meth:`find_one() <dataset.Table.find_one>`::
|
||||||
|
|
||||||
# All users from China
|
# All users from China
|
||||||
users = table.find(country='China')
|
chinese_users = table.find(country='China')
|
||||||
|
|
||||||
# Get a specific user
|
# Get a specific user
|
||||||
john = table.find_one(name='John Doe')
|
john = table.find_one(name='John Doe')
|
||||||
@ -126,8 +126,8 @@ The :py:meth:`query() <dataset.Table.query>` method can also be used to
|
|||||||
access the underlying `SQLAlchemy core API <http://docs.sqlalchemy.org/ru/latest/orm/query.html#the-query-object>`_, which allows for the
|
access the underlying `SQLAlchemy core API <http://docs.sqlalchemy.org/ru/latest/orm/query.html#the-query-object>`_, which allows for the
|
||||||
programmatic construction of more complex queries::
|
programmatic construction of more complex queries::
|
||||||
|
|
||||||
table = db['users'].table
|
table = db['user'].table
|
||||||
statement = table.select(table.c.name.like('%Snoopy%'))
|
statement = table.select(table.c.name.like('%John%'))
|
||||||
result = db.query(statement)
|
result = db.query(statement)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user