A
A
Artyom2018-04-09 17:10:24
MySQL
Artyom, 2018-04-09 17:10:24

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()

This raises an error:
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'")

And if you run this code:
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()

Then we get
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')

Please help to understand what the problem is and, if possible, fix it.
ATTENTION! I registered as ANONYMOUS and don't know how to enter my username and password. Tell me please!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
Twelfth Doctor, 2018-04-09
@TheProgrammer256

You need to specify the username in the connection
user - username, db - database name.

S
Stanislav Pugachev, 2018-04-09
@Stqs

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?

A
Artyom, 2018-04-09
@TheProgrammer256

Please clarify how I can provide a username and password, given that I am anonymous.

R
Ramzi_bullet, 2020-02-07
@Ramzi_bullet

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 question

Ask a Question

731 491 924 answers to any question