@@ -12,7 +12,7 @@ implement |DBAPI|.
12
12
13
13
Below is a simple example of a typical use of MariaDB Connector/Python
14
14
15
- .. testcode ::
15
+ .. testsetup ::
16
16
17
17
import mariadb
18
18
@@ -28,18 +28,42 @@ Below is a simple example of a typical use of MariaDB Connector/Python
28
28
connection= mariadb.connect(**conn_params)
29
29
30
30
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)
31
58
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()
36
60
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 "))
40
64
41
65
# retrieve data
42
- cursor.execute("SELECT id, first_name, last_name FROM mytest ")
66
+ cursor.execute("SELECT name, country_code, capital FROM countries ")
43
67
44
68
# print content
45
69
row= cursor.fetchone()
@@ -53,7 +77,7 @@ Below is a simple example of a typical use of MariaDB Connector/Python
53
77
54
78
.. testoutput ::
55
79
56
- 1 Robert Redford
80
+ Germany GER Berlin
57
81
58
82
59
83
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
100
124
101
125
cursor= connection.cursor()
102
126
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)
113
130
114
- # Since autocommit is off by default, we need to commit last transaction
115
131
connection.commit()
116
132
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",)
119
136
cursor.execute(sql, data)
120
137
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
133
138
connection.commit()
134
139
135
- # free resources
136
140
cursor.close()
137
141
connection.close()
142
+
143
+
138
144
139
145
140
146
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
156
162
connection= mariadb.connect(**conn_params)
157
163
158
164
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
165
175
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")]
170
189
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",)]
178
194
cursor.executemany(sql, data)
179
195
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
+
180
204
When using executemany(), there are a few restrictions:
181
205
- All tuples must have the same types as in first tuple. E.g. the parameter [(1),(1.0)] or [(1),(None)] are invalid.
182
206
- Special values like None or column default value needs to be indicated by an indicator.
0 commit comments