Answer the question
In order to leave comments, you need to log in
Python + MySQL: why is the error happening?
I am trying to work with the database using the following code:
import MySQLdb
conn = MySQLdb.connect(host='localhost', db='main_database', charset='utf8')
cursor = conn.cursor()
for i in range(1, 401):
cursor.execute(f"INSERT INTO lessons (id, title, text1, image1, text2, image2) VALUES ({i}, '', '', '', '');")
conn.close()
File "C:/Users/Дгиф/PycharmProjects/LubaProject/script_table.py", line 3, in <module>
conn = MySQLdb.connect(db='main_database', charset='utf8')
File "C:\Users\Дгиф\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\__init__.py", line 86, in Connect
return Connection(*args, **kwargs)
File "C:\Users\Дгиф\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1044, "Access denied for user ''@'localhost' to database 'main_database'")
import MySQLdb
conn = MySQLdb.connect('localhost', 'main_database')
cursor = conn.cursor()
for i in range(1, 401):
cursor.execute(f"INSERT INTO lessons (id, title, text1, image1, text2, image2) VALUES ({i}, '', '', '', '');")
conn.close()
Traceback (most recent call last):
File "C:/Users/Дгиф/PycharmProjects/LubaProject/script_table.py", line 6, in <module>
cursor.execute(f"INSERT INTO lessons (id, title, text1, image1, text2, image2) VALUES ({i}, '', '', '', '');")
File "C:\Users\Дгиф\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "C:\Users\Дгиф\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:\Users\Дгиф\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "C:\Users\Дгиф\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "C:\Users\Дгиф\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\Users\Дгиф\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\connections.py", line 277, in query
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1046, 'No database selected')
Answer the question
In order to leave comments, you need to log in
You need to specify the username in the connection
user - username, db - database name.
I would start with documentation
http://mysql-python.sourceforge.net/MySQLdb.html#f...
pass all arguments host, login, password, and database name obviously
what happens?
Please clarify how I can provide a username and password, given that I am anonymous.
Friends, I have a task at work to parse data from a database from a remote server, log in to it via VPN, enter a login / password for authorization in the database, but the 2003 error ""Can't connect to MySQL server on --server--" Tell me if anyone knows what the problem is.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question