|
| 1 | +Accessing the database |
| 2 | +====================== |
| 3 | + |
| 4 | +Web.py provides a simple and uniform interface to the database that you want to work with, whether it is PostgreSQL, MySQL, SQLite or any other. It doesn't try to build layers between you and your database. Rather, it tries to make it easy to perform common tasks, and get out of your way when you need to do more advanced things. |
| 5 | + |
| 6 | + |
| 7 | +Create a database object |
| 8 | +------------------------ |
| 9 | + |
| 10 | +You could create a database object with `web.database()`. Make sure that you have appropriate database library installed. |
| 11 | + |
| 12 | +PostgreSQL |
| 13 | +`````````` |
| 14 | +Database Library: `psycopg2` |
| 15 | +:: |
| 16 | + |
| 17 | + db = web.database(dbn='postgres', db='dbname', user='username', pw='password') |
| 18 | + |
| 19 | +MySQL |
| 20 | +````` |
| 21 | +Database Library: `MySQLdb` |
| 22 | +:: |
| 23 | + |
| 24 | + db = web.database(dbn='mysql', db='dbname', user='username', pw='password') |
| 25 | + |
| 26 | + |
| 27 | +SQLite |
| 28 | +`````` |
| 29 | + |
| 30 | +Database Library: `sqlite3` |
| 31 | +:: |
| 32 | + |
| 33 | + db = web.database(dbn='sqlite', db='dbname') |
| 34 | + |
| 35 | + |
| 36 | +Multiple databases |
| 37 | +`````````````````` |
| 38 | + |
| 39 | +Working with more databases is not at all difficult with web.py. Here's what you do. |
| 40 | + |
| 41 | +:: |
| 42 | + |
| 43 | + db1 = web.database(dbn='postgres', db='dbname1', user='username1', pw='password2') |
| 44 | + db2 = web.database(dbn='postgres', db='dbname2', user='username2', pw='password2') |
| 45 | + |
| 46 | +And use `db1`, `db2` to access those databases respectively. |
| 47 | + |
| 48 | + |
| 49 | +Operations |
| 50 | +---------- |
| 51 | +`web.database()` returns an object which provide you all the functionality to insert, select, update and delete data from your database. For each of the methods on `db` below, you can pass `_test=True` to see the SQL statement rather than executing it. |
| 52 | + |
| 53 | + |
| 54 | +Inserting |
| 55 | +````````` |
| 56 | +:: |
| 57 | + |
| 58 | + # Insert an entry into table 'user' |
| 59 | + userid = db.insert('user', firstname="Bob", lastname="Smith", joindate=web.SQLLiteral("NOW()")) |
| 60 | + |
| 61 | + |
| 62 | +The first argument is the table name and the rest of them are set of named arguments which represent the fields in the table. If values are not given, the database may create default values or issue a warning. |
| 63 | + |
| 64 | +For bulk insertion rather than inserting record by record, use `multiple_insert` rather. |
| 65 | + |
| 66 | +Bulk Inserting |
| 67 | +`````````````` |
| 68 | + |
| 69 | + |
| 70 | +Selecting |
| 71 | +````````` |
| 72 | +To select `all` the rows from the `user` table, you would simply do |
| 73 | + |
| 74 | +:: |
| 75 | + |
| 76 | + users = db.select('user') |
| 77 | + |
| 78 | +For the real world use cases, `select` method takes `vars`, `what`, `where`, `order`, `group`, `limit`, `offset`, and `_test` optional parameters. |
| 79 | + |
| 80 | +:: |
| 81 | + |
| 82 | + users = db.select('users', where="id>100") |
| 83 | + |
| 84 | +To prevent SQL injection attacks, you can use `$key` in where clause and pass the `vars` which has { 'key': value }. |
| 85 | + |
| 86 | +:: |
| 87 | + |
| 88 | + vars = dict(name="Bob") |
| 89 | + results = db.select('users', where="name = $name", vars=vars, _test=True) |
| 90 | + >>> results |
| 91 | + <sql: "SELECT * FROM users WHERE name = 'Bob'"> |
| 92 | + |
| 93 | + |
| 94 | +Advanced querying |
| 95 | +````````````````` |
| 96 | + |
| 97 | +Many a times, there is more to do with the database, rather than the simple operations which can be done by `insert`, `select`, `delete` and `update` - Things like your favorite (or scary) joins, counts etc. All these are possible with `query` method, which also takes `vars`. |
| 98 | + |
| 99 | +:: |
| 100 | + |
| 101 | + results = db.query("SELECT COUNT(*) AS total_users FROM users") |
| 102 | + print results[0].total_users # prints number of entries in 'users' table |
| 103 | + |
| 104 | +Joining tables |
| 105 | +:: |
| 106 | + |
| 107 | + results = db.query("SELECT * FROM entries JOIN users WHERE entries.author_id = users.id") |
| 108 | + |
| 109 | + |
| 110 | +Updating |
| 111 | +```````` |
| 112 | +The `update` method accepts same kind of arguments as Select. It returns the number of rows updated. |
| 113 | + |
| 114 | +:: |
| 115 | + |
| 116 | + num_updated = db.update('users', where="id = 10", firstname = "Foo") |
| 117 | + |
| 118 | +Deleting |
| 119 | +```````` |
| 120 | +The `delete` method returns the number of rows deleted. It also accepts "using" and "vars" parameters. See ``Selecting`` for more details on `vars`. |
| 121 | + |
| 122 | +:: |
| 123 | + |
| 124 | + num_deleted = db.delete('users', where="id=10") |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
0 commit comments