728x90
#10.5.4 MySQLCursor.execute() Method
cursor.execute(operation, params=None, multi=False)
iterator = cursor.execute(operation, params=None, multi=True)
This method executes the given database operation (query or command).
The parameters found in the tuple or dictionary params are bound to the variables in the operation. Specify variables using %s or %(name)s parameter style (that is, using format or pyformat style). execute() returns an iterator if multi is True.
# excute operation
insert_stmt = (
"INSERT INTO employees (emp_no, first_name, last_name, hire_date) "
"VALUES (%s, %s, %s, %s)"
)
data = (2, 'Jane', 'Doe', datetime.date(2012, 3, 23))
cursor.execute(insert_stmt, data)
select_stmt = "SELECT * FROM employees WHERE emp_no = %(emp_no)s"
cursor.execute(select_stmt, { 'emp_no': 2 })
#Example
operation = 'SELECT 1; INSERT INTO t1 VALUES (); SELECT 2'
for result in cursor.execute(operation, multi=True):
if result.with_rows:
print("Rows produced by statement '{}':".format(
result.statement))
print(result.fetchall())
else:
print("Number of rows affected by statement '{}': {}".format(
result.statement, result.rowcount))
๋ฐ์ํ
'๐ข๏ธ Database' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL] Count ํจ์, Sequel vs SQL (0) | 2023.08.19 |
---|---|
Apache Parquet(ํ์ผ์ด) (0) | 2023.08.14 |
[์์ธ] Mysql database ์ ๋ ฌ (0) | 2023.07.31 |
[Mysql] python executemany ์ฌ์ฉ์ TypeError: not all arguments converted during string ... (0) | 2022.03.23 |
[Mysql] Pymysql INSERT ์ฟผ๋ฆฌ ์์ฑ๊ธฐ (0) | 2022.03.21 |