Python Forum
Need Help with connecting to mysql from txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help with connecting to mysql from txt file
#1
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()
Larz60+ write Mar-23-2021, 12:21 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
#2
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.
#3
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?
#4
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.
#5
I close this thread, keep the discussion in the other thread
https://python-forum.io/Thread-how-to-co...es-in-text
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs



Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 683 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,089 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  Code import .CSV file to MySQL table rtakle 4 2,865 Apr-30-2020, 03:16 PM
Last Post: anbu23
  MySql - Loop - CHeck File gcclinux 1 2,093 Nov-30-2019, 11:51 AM
Last Post: ThomasL
  Read CSV file into MySQL and use executemany instead of execute bluethundr 1 4,204 May-04-2019, 07:35 AM
Last Post: MvGulik
  Import Large CSV File into MySQL bluethundr 2 7,148 Apr-28-2019, 06:35 PM
Last Post: Yoriz
  Connecting to MySQL Dean Stackhouse 4 9,007 Oct-05-2016, 05:57 PM
Last Post: Dean Stackhouse

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020