@@ -72,7 +72,7 @@ Python versions::
72
72
$ tox
73
73
74
74
75
- Exapmle
75
+ Example
76
76
-------
77
77
78
78
The following examples make use of a simple table
@@ -90,7 +90,7 @@ The following examples make use of a simple table
90
90
91
91
.. code :: python
92
92
93
- import pymysql
93
+ import pymysql.cursors
94
94
95
95
# Connect to the database
96
96
connection = pymysql.connect(host = ' localhost' ,
@@ -102,14 +102,17 @@ The following examples make use of a simple table
102
102
try :
103
103
with connection.cursor() as cursor:
104
104
# 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' ))
109
107
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:
110
113
# Read a single record
111
114
sql = " SELECT `id`, `password` FROM `users` WHERE `email`=%s "
112
- cursor.execute(sql, ' webmaster@python.org' )
115
+ cursor.execute(sql, ( ' webmaster@python.org' , )
113
116
result = cursor.fetchone()
114
117
print (result)
115
118
finally :
@@ -119,11 +122,7 @@ This example will print:
119
122
120
123
.. code:: python
121
124
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 }
127
126
128
127
129
128
Resources
0 commit comments