Python Forum

Full Version: Need Help with connecting to mysql from txt file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello i was trying to connect by string or variable first but it didnt seem to hold.
i have checked multiple forums and did lots of google but i dont seem to figure it out please help me in mij proces

import MySQLdb
from concurrent.futures import ThreadPoolExecutor
import os, sys

inp = open('database-sorted.txt', 'r', encoding='utf-8')
content_list = [line.rstrip('\n') for line in inp]
print(inp)
connection = eval('MySQLdb.connect({})'.format(inp.readlines().strip()))

inp.close()

print (connection)
a second way i did try was this god even with str and +str(blabla)+
build = '\nDB_HOST: '+str(db_host)+'\ndb_database: '+str(db_database)+'\nDB_USERNAME: '+str(db_username)+'\nDB_PASSWORD: '+str(db_password)
remover = str(build).replace('\r', '')
save = open('Results/blblbl.txt', 'a')
save.write(remover+'\n\n')
save.close()

cnx = mysql.connector.connect(user='{db_user}', password='{db_password}',
                                host='{db_host}', database='{db_database}')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  print("You are connected!")
  cnx.close()
first, you don't need the concurrent.futures package, that's for multi tasking
remove line 1:
from concurrent.futures import ThreadPoolExecutor
you don't want to be using eval either.
see this which will show you how to connect: https://dev.mysql.com/doc/connector-pyth...cting.html

remember, when in doubt, google is your friend.
thnx for taking the time i just started with learning how to code python.
i did manage to read the first line of my txt with databases in it.

import MySQLdb
import sys
import os


inp = open('data.txt', 'r', encoding='utf-8')

connection = eval('MySQLdb.connect({})'.format(inp.readline().strip()))

print(inp.readline().strip())
But The Problem is i need it to Read all lines i have sorted in the file like this host="", user="", passwd="", db=""



i get

Error:
C:\Users\Gebruiker\Downloads\V1>python3 dbs_livechecker.py Traceback (most recent call last): File "C:\Users\Gebruiker\Downloads\V1\dbs_livechecker.py", line 8, in <module> connection = eval('MySQLdb.connect({})'.format(inp.readline().strip())) File "<string>", line 1, in <module> File "C:\Users\Gebruiker\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\MySQLdb\__init__.py", line 130, in Connect return Connection(*args, **kwargs) File "C:\Users\Gebruiker\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\MySQLdb\connections.py", line 185, in __init__ super().__init__(*args, **kwargs2) MySQLdb._exceptions.OperationalError: (2002, "Can't connect to MySQL server on 'localhost' (10061)")
wich is a good thing right ? because it worked! but it stops after that first line. you know how to add right function or correct way to make the file run until the end of file i input?
It would be better to learn SQL, many tutorials available.
There are also packages for this see: https://pypi.org/search/?q=mysql
fix your errors.
It's not working properly until none found.
Read the docs.
I close this thread, keep the discussion in the other thread
https://python-forum.io/Thread-how-to-co...es-in-text