cursor.execute(sql, val)
时间: 2023-11-13 08:03:03 浏览: 91
This is a method call to execute a SQL statement using a cursor object in Python. The first argument, sql, is a string containing the SQL statement to be executed. The second argument, val, is a tuple or list containing any parameters to be substituted into the SQL statement.
For example, if we had a SQL statement that looked like this:
```
INSERT INTO customers (name, email, phone) VALUES (%s, %s, %s);
```
And we wanted to insert a new customer with the name "John Smith", email "john.smith@example.com", and phone number "555-1234", we could use the following code:
```
sql = "INSERT INTO customers (name, email, phone) VALUES (%s, %s, %s);"
val = ("John Smith", "john.smith@example.com", "555-1234")
cursor.execute(sql, val)
```
This would execute the SQL statement with the parameter values substituted in.
阅读全文