Skip to content

Commit 93241e4

Browse files
committed
Documentation
Added testsetup for sphinx' doctest
1 parent fb175c7 commit 93241e4

19 files changed

+360
-219
lines changed

doc/source/usage.rst

Lines changed: 77 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ implement |DBAPI|.
1212

1313
Below is a simple example of a typical use of MariaDB Connector/Python
1414

15-
.. testcode::
15+
.. testsetup::
1616

1717
import mariadb
1818

@@ -28,18 +28,42 @@ Below is a simple example of a typical use of MariaDB Connector/Python
2828
connection= mariadb.connect(**conn_params)
2929
3030
cursor= connection.cursor()
31+
cursor.execute("CREATE OR REPLACE TABLE `countries` ("
32+
"`id` int(10) unsigned NOT NULL AUTO_INCREMENT,"
33+
"`name` varchar(50) NOT NULL,"
34+
"`country_code` char(3) NOT NULL,"
35+
"`capital` varchar(50) DEFAULT NULL,"
36+
"PRIMARY KEY (`id`),"
37+
"KEY `name` (`name`),"
38+
"KEY `capital` (`capital`)"
39+
") ENGINE=InnoDB DEFAULT CHARSET=latin1")
40+
41+
cursor.close()
42+
connection.close()
43+
44+
.. testcode::
45+
46+
import mariadb
47+
48+
# connection parameters
49+
conn_params= {
50+
"user" : "example_user",
51+
"password" : "GHbe_Su3B8",
52+
"host" : "localhost",
53+
"database" : "test"
54+
}
55+
56+
# Establish a connection
57+
connection= mariadb.connect(**conn_params)
3158
32-
# Create a database table
33-
cursor.execute("DROP TABLE IF EXISTS mytest")
34-
cursor.execute("CREATE TABLE mytest(id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
35-
"first_name VARCHAR(100), last_name VARCHAR(100))")
59+
cursor= connection.cursor()
3660

37-
# Populate table with some data
38-
cursor.execute("INSERT INTO mytest(first_name, last_name) VALUES (?,?)",
39-
("Robert", "Redford"))
61+
# Populate countries table with some data
62+
cursor.execute("INSERT INTO countries(name, country_code, capital) VALUES (?,?,?)",
63+
("Germany", "GER", "Berlin"))
4064

4165
# retrieve data
42-
cursor.execute("SELECT id, first_name, last_name FROM mytest")
66+
cursor.execute("SELECT name, country_code, capital FROM countries")
4367

4468
# print content
4569
row= cursor.fetchone()
@@ -53,7 +77,7 @@ Below is a simple example of a typical use of MariaDB Connector/Python
5377

5478
.. testoutput::
5579

56-
1 Robert Redford
80+
Germany GER Berlin
5781

5882

5983
Before MariaDB Connector/Python can be used, the MariaDB Connector/Python module must be
@@ -100,41 +124,23 @@ Since |MCP| uses binary protocol, escaping strings or binary data like in other
100124
101125
cursor= connection.cursor()
102126

103-
cursor.execute("DROP TABLE IF EXISTS books")
104-
cursor.execute("CREATE TABLE books(id int not null auto_increment primary key,\
105-
book_name VARCHAR(100), author_name VARCHAR(50), price DECIMAL(10,2))")
106-
107-
108-
# insert multiple
109-
books= [("Dream of the Red Chamber", "Cao Xueqin", 13.90),
110-
("The Little Prince", "Antoine de Saint-Exupéry", 9.40)]
111-
sql= "INSERT INTO books (book_name, author_name, price) VALUES (?, ?, ?)"
112-
cursor.executemany(sql, books);
127+
sql= "INSERT INTO countries (name, country_code, capital) VALUES (?,?,?)"
128+
data= ("Germany", "GER", "Berlin")
129+
cursor.execute(sql, data)
113130

114-
# Since autocommit is off by default, we need to commit last transaction
115131
connection.commit()
116132

117-
sql= "INSERT INTO books (book_name, author_name, price) VALUES (?, ?, ?)"
118-
data= ("The Lord of the Rings", "J.R.R. Tolkien", 18.99)
133+
# delete last entry
134+
sql= "DELETE FROM countries WHERE country_code=?"
135+
data= ("GER",)
119136
cursor.execute(sql, data)
120137

121-
# update
122-
sql= "UPDATE books SET price=? WHERE book_name=?"
123-
data= (14.90, "Dream of the Red Chamber")
124-
cursor.execute(sql, data)
125-
126-
# delete
127-
sql= "DELETE FROM books WHERE id=?"
128-
data= (2,) # Don't forget a comma at the end!
129-
cursor.execute(sql, data)
130-
131-
# by default autocommit is off, so we need to commit
132-
# our transactions
133138
connection.commit()
134139

135-
# free resources
136140
cursor.close()
137141
connection.close()
142+
143+
138144

139145

140146
Often there is a requirement to update, delete or insert multiple records. This could be done be using :func:`~execute` in
@@ -156,27 +162,45 @@ a loop, but much more effective is using the :func:`executemany` method, especia
156162
connection= mariadb.connect(**conn_params)
157163
158164
cursor= connection.cursor()
159-
160-
# update
161-
sql= "UPDATE books SET price=? WHERE book_name=?"
162-
data= [(14.90, "Dream of the Red Chamber"),
163-
(22.30, "The Master and Margarita"),
164-
(17.10, "And Then There Were None")]
165+
sql= "INSERT INTO countries (name, country_code, capital) VALUES (?,?,?)"
166+
167+
data= [("Ireland", "IE", "Dublin"),
168+
("Italy", "IT", "Rome"),
169+
("Malaysia", "MY", "Kuala Lumpur"),
170+
("France", "FR", "Paris"),
171+
("Iceland", "IS", "Reykjavik"),
172+
("Nepal", "NP", "Kathmandu")]
173+
174+
# insert data
165175
cursor.executemany(sql, data)
166-
167-
# delete
168-
sql= "DELETE FROM books WHERE id=?"
169-
data= [(4034,),(12001,),(230,)]
176+
177+
# Since autocommit is off by default, we need to commit last transaction
178+
connection.commit()
179+
180+
# Instead of 3 letter country-code, we inserted 2 letter country code, so
181+
# let's fix this mistake by updating data
182+
sql= "UPDATE countries SET country_code=? WHERE name=?"
183+
data= [("Ireland", "IRL"),
184+
("Italy", "ITA"),
185+
("Malaysia", "MYS"),
186+
("France", "FRA"),
187+
("Iceland", "ISL"),
188+
("Nepal", "NPL")]
170189
cursor.executemany(sql, data)
171-
172-
#insert
173-
sql= "INSERT INTO books (book_name, author_name, price) VALUES (?, ?, ?)"
174-
data= [("The Lord of the Rings", "J.R.R. Tolkien", 18.99),
175-
("Le Petit Prince", "Antoine de Saint-Exupéry", 22.40),
176-
("Dream of the Red Chamber", "Cao Xueqin", 16.90),
177-
("The Adventures of Pinocchio", "Carlo Collodi", 17.10)]
190+
191+
# Now let's delete all non European countries
192+
sql= "DELETE FROM countries WHERE name=?"
193+
data= [("Malaysia",), ("Nepal",)]
178194
cursor.executemany(sql, data)
179195

196+
# by default autocommit is off, so we need to commit
197+
# our transactions
198+
connection.commit()
199+
200+
# free resources
201+
cursor.close()
202+
connection.close()
203+
180204
When using executemany(), there are a few restrictions:
181205
- All tuples must have the same types as in first tuple. E.g. the parameter [(1),(1.0)] or [(1),(None)] are invalid.
182206
- Special values like None or column default value needs to be indicated by an indicator.

0 commit comments

Comments
 (0)