@@ -81,3 +81,67 @@ server.
81
81
Inserting Data
82
82
--------------
83
83
84
+ Let's take basic example of :meth: `Cursor.execute ` method::
85
+
86
+ import asyncio
87
+ import aiomysql
88
+
89
+
90
+ async def test_example_execute(loop):
91
+ conn = await aiomysql.connect(host='127.0.0.1', port=3306,
92
+ user='root', password='',
93
+ db='test_pymysql', loop=loop)
94
+
95
+ cur = await conn.cursor()
96
+ async with conn.cursor() as cur:
97
+ await cur.execute("DROP TABLE IF EXISTS music_style;")
98
+ await cur.execute("""CREATE TABLE music_style
99
+ (id INT,
100
+ name VARCHAR(255),
101
+ PRIMARY KEY (id));""")
102
+ await conn.commit()
103
+
104
+ # insert 3 rows one by one
105
+ await cur.execute("INSERT INTO music_style VALUES(1,'heavy metal')")
106
+ await cur.execute("INSERT INTO music_style VALUES(2,'death metal');")
107
+ await cur.execute("INSERT INTO music_style VALUES(3,'power metal');")
108
+ await conn.commit()
109
+
110
+ conn.close()
111
+
112
+
113
+ loop = asyncio.get_event_loop()
114
+ loop.run_until_complete(test_example_execute(loop))
115
+
116
+ Please note that you need to manually call :func: `commit() ` bound to your :term: `Connection ` object, because by default it's set to ``False `` or in :meth: `aiomysql.connect() ` you can transfer addition keyword argument ``autocommit=True ``.
117
+
118
+ Example with ``autocommit=False ``::
119
+
120
+ import asyncio
121
+ import aiomysql
122
+
123
+
124
+ async def test_example_execute(loop):
125
+ conn = await aiomysql.connect(host='127.0.0.1', port=3306,
126
+ user='root', password='',
127
+ db='test_pymysql', loop=loop,
128
+ autocommit=True)
129
+
130
+ cur = await conn.cursor()
131
+ async with conn.cursor() as cur:
132
+ await cur.execute("DROP TABLE IF EXISTS music_style;")
133
+ await cur.execute("""CREATE TABLE music_style
134
+ (id INT,
135
+ name VARCHAR(255),
136
+ PRIMARY KEY (id));""")
137
+
138
+ # insert 3 rows one by one
139
+ await cur.execute("INSERT INTO music_style VALUES(1,'heavy metal')")
140
+ await cur.execute("INSERT INTO music_style VALUES(2,'death metal');")
141
+ await cur.execute("INSERT INTO music_style VALUES(3,'power metal');")
142
+
143
+ conn.close()
144
+
145
+
146
+ loop = asyncio.get_event_loop()
147
+ loop.run_until_complete(test_example_execute(loop))
0 commit comments