Skip to content

Commit 3074c1d

Browse files
committed
Fix example
1 parent 7f39460 commit 3074c1d

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

README.rst

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Python versions::
7272
$ tox
7373

7474

75-
Exapmle
75+
Example
7676
-------
7777

7878
The following examples make use of a simple table
@@ -90,7 +90,7 @@ The following examples make use of a simple table
9090
9191
.. code:: python
9292
93-
import pymysql
93+
import pymysql.cursors
9494
9595
# Connect to the database
9696
connection = pymysql.connect(host='localhost',
@@ -102,14 +102,17 @@ The following examples make use of a simple table
102102
try:
103103
with connection.cursor() as cursor:
104104
# Create a new record
105-
sql = ("INSERT INTO `users` (`email`, `password`) "
106-
"VALUES ('webmaster@python.org', 'very-secret');")
107-
cursor.execute(sql)
108-
connection.commit()
105+
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
106+
cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
109107
108+
# connection is not autocommit by default. So you must commit to save
109+
# your changes.
110+
connection.commit()
111+
112+
with connection.cursor() as cursor:
110113
# Read a single record
111114
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
112-
cursor.execute(sql, 'webmaster@python.org')
115+
cursor.execute(sql, ('webmaster@python.org',)
113116
result = cursor.fetchone()
114117
print(result)
115118
finally:
@@ -119,11 +122,7 @@ This example will print:
119122
120123
.. code:: python
121124
122-
{'password': 'very-secret', 'id': 4}
123-
124-
125-
assuming the first matching recording has id 4 and password 'very-secret'.
126-
If there is no match, `None` is printed.
125+
{'password': 'very-secret', 'id': 1}
127126
128127
129128
Resources

0 commit comments

Comments
 (0)