import pymysql
时间: 2023-08-28 08:08:14 浏览: 97
As an AI language model, I do not have the capability to establish database connections or run queries. However, I can provide you with an example code for establishing a MySQL database connection using Python and pymysql library.
To establish a MySQL database connection, you need to provide the following details:
- Hostname or IP address of the MySQL server
- Port number
- Username and password
- Database name (optional)
Here is an example code for establishing a MySQL database connection using pymysql library:
```
import pymysql
# MySQL database connection details
host = "localhost"
port = 3306
user = "root"
password = "password"
database = "mydb"
# Establish database connection
try:
conn = pymysql.connect(host=host, port=port, user=user, password=password, db=database)
print("Database connection successful!")
except Exception as e:
print("Error connecting to database: ", e)
```
In the above code, we first import the pymysql library. Then, we provide the MySQL database connection details such as hostname, port number, username, password, and database name (optional).
Next, we use the `pymysql.connect()` method to establish a database connection. If the connection is successful, we print a message indicating that the connection was successful. If there is an error in connecting to the database, we print the error message.
阅读全文