Skip to content
Newer
Older
100644 43 lines (27 sloc) 1.18 KB
730296d @btipling creating insert page
btipling authored Apr 20, 2008
1 ---
2 layout: default
aa1fb95 edit
anonymous authored Sep 20, 2009
3 title: Insert: Adding a new entry to a database
730296d @btipling creating insert page
btipling authored Apr 20, 2008
4 ---
5
aa1fb95 edit
anonymous authored Sep 20, 2009
6 # Insert: Adding a new entry to a database
730296d @btipling creating insert page
btipling authored Apr 20, 2008
7
8 ###Problem: You want to add data to a database
9
10 ###Solution:
11
12 With version 0.3, databases are defined like this:
13
14 db = web.database(dbn='postgres', db='mydata', user='dbuser', pw='')
15
16 Once the database is defined as such, performing insert can be performed like this:
17
2c08238 @tobarja edit
tobarja authored Dec 7, 2008
18 # Insert an entry into table 'mytable'
730296d @btipling creating insert page
btipling authored Apr 20, 2008
19 sequence_id = db.insert('mytable', firstname="Bob",lastname="Smith",joindate=web.SQLLiteral("NOW()"))
20
21 The insert statement takes the following keyword arguments:
22
23 tablename
24 seqname
25 _test
81cb0ca @btipling edit
btipling authored Apr 20, 2008
26 \**values
730296d @btipling creating insert page
btipling authored Apr 20, 2008
27
28
29
30 ##tablename
31 The name of the table in your database to which you would like to add data to.
32
33 ##seqname
34 An optional argument, the default value is None. Set `seqname` to the ID if it's not the default, or to `False`.
35
36 ##_test
37 The _test variable lets you see the SQL produced by the statement:
38
39 results = db.select('mytable', offset=10, _test=True)
40 ><sql: 'SELECT * FROM mytable OFFSET 10'>
41
ee4d358 @btipling edit
btipling authored Apr 20, 2008
42 ##\**values
aa1fb95 edit
anonymous authored Sep 20, 2009
43 A set of named arguments that represent the fields in your table. If values are not given, the database may create default values or issue a warning.
Something went wrong with that request. Please try again.