Solve python error: mysql.connector.errors.ProgrammingError: 1046 (3D000): No database selected

1. The error is as follows

2. Check the code

 3. Analysis errors

According to the error message you provided, you encountered a MySQL connector error with error code 1046 and error message "No database selected". This error usually occurs when you have not selected a database before attempting to perform a SQL query or operation.

To get around this, you can specify a default database when connecting to MySQL. When using the Python mysql.connectorlibrary to connect, you can specify the default database in the connection parameters:

import mysql.connector # Connect to the MySQL server and specify the default database cnx = mysql.connector.connect(user='username', password='password', host='localhost', database='mydatabase')

Be sure to replace username, password, localhost, and mydatabasewith your actual connection parameters.

Before executing the SQL statement, make sure you have selected the correct database. For example, the following statement can be used to select a database before creating a table: cursor.execute("USE mydatabase;")

4. Modify the code

 5. View the running results

 Note: If the newly created table is created successfully, create a new table again, otherwise the operation will report an error that the table already exists.

Hope to help you, thank you~

 

Guess you like

Origin blog.csdn.net/Along_168163/article/details/132036552