Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 43 lines (27 sloc) 1.209 kb
730296d Bjorn Tipling creating insert page
btipling authored
1 ---
2 layout: default
aa1fb95 edit
anonymous authored
3 title: Insert: Adding a new entry to a database
730296d Bjorn Tipling creating insert page
btipling authored
4 ---
5
aa1fb95 edit
anonymous authored
6 # Insert: Adding a new entry to a database
730296d Bjorn Tipling creating insert page
btipling authored
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 Andrew Thompson edit
tobarja authored
18 # Insert an entry into table 'mytable'
730296d Bjorn Tipling creating insert page
btipling authored
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 Bjorn Tipling edit
btipling authored
26 \**values
730296d Bjorn Tipling creating insert page
btipling authored
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 Bjorn Tipling edit
btipling authored
42 ##\**values
aa1fb95 edit
anonymous authored
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.