Hello guys I am currently working in a python project at my school. First I want to make clear that I'm not a python programmer (I was just called to put out the flames in this project because no one else would and I was brave enough to say yes).
I have the following problem here. I have to write a method that connects to an existing localhost MySQL database (I'm using connector version 1.0.12 and python 2.6) and then does pretty basic stuff. The parameters are sent by a GTK-written GUI (I didn't write that interface). So I wrote my method like this:
def compMySQL(self, user, database, password, db_level, table_level, column_level):
sql_page_textview = self.mainTree.get_widget('sql_text_view')
sql_page_textview.modify_font(pango.FontDescription("courier 10"))
sql_page_buffer = sql_page_textview.get_buffer()
#Gonna try connecting to DB
try:
print("Calling conn with U:{0} P:{1} DB:{2}".format(user,password,database))
cnxOMC = mysql.connector.connect(user, password,'localhost',database)
except:
print "Error: Database connection failed. User name or Database name may be wrong"
return
#More code ...
But when I run my code I get this:
Calling conn with U:root P:PK17LP12r DB:TESTERS
Error: Database connection failed. User name or Database name may be wrong
And I don't know why, since the arguments sent are the same arguments that get printed (telling me that the GUI the other guy coded works fine) and they are valid login parameters. If I hardcode the login parameters directly insetad of using the GUI everything goes ok and the functions executes properly; the following code executes nice and smooth:
def compMySQL(self, user, database, password, db_level, table_level, column_level):
sql_page_textview = self.mainTree.get_widget('sql_text_view')
sql_page_textview.modify_font(pango.FontDescription("courier 10"))
sql_page_buffer = sql_page_textview.get_buffer()
#Gonna try hardcoding
try:
#print("Calling conn with U:{0} P:{1} DB:{2}".format(user,password,database))
cnxOMC = mysql.connector.connect(user="root", password='PK17LP12r', host='localhost', database='TESTERS')
print 'No prob with conn'
except:
print "Error: Database connection failed. User name or Database name may be wrong"
return
#more code ...
Console output:
No prob with conn
Any ideas guys? This one is killing me. I'm just learning Python but I imagine the problem to be something very easy for a seasoned python developer so any help would be strongly appreciated.
Thanks in advance.
The difference between the two versions is not that you are hard coding the parameters in the second one, it is that you are calling by keyword args rather than positional ones. The docs for MySQL connector don't seem to give the actual positional order, and there's no reason to think they are in the order you've given, so looks like you should always call by kwarg:
cnxOMC = mysql.connector.connect(user=user, password=password,host=host,database=database)
Related
I would like to run a large python script programmed by someone else (https://github.com/PatentsView/PatentsView-Disambiguation to be precise). At numerous times during the execution of this code, python connects to a mysql database in order to read some data. The problem is that I
a) cannot get a mysql installation on the server I use. Being only a guest at the institution whose computers I use, I cannot really influence IT to change this
b) would like to alter the code as little as possible, since I have very little python experience
The original code uses a function granted_table() that returns a mysql.connector.connect(...), where host, user, password etc. are given in the dots. My idea is to switch out this function with one that reads in TSV files instead, which I have all stored on my machine. This way, I do not have to go over the long script and fiddle around with it (I have almost no experience with python and might mess something up without realizing it).
I have tried a few things already and it almost works, but not quite. The reproducible example follows:
First, there is the original function that accesses mysql, which has to go
def granted_table(config):
if config['DISAMBIGUATION']['granted_patent_database'].lower() == 'none':
logging.info('[granted_table] no db given')
return None
else:
logging.info('[granted_table] trying to connect to %s', config['DISAMBIGUATION']['granted_patent_database'])
return mysql.connector.connect(host=config['DATABASE']['host'],
user=config['DATABASE']['username'],
password=config['DATABASE']['password'],
database=config['DISAMBIGUATION']['granted_patent_database'])
Second, the function that tries to call the above function to read in data (I shortened and simplified it but kept the structure as I understand it. This version of the function actually only prints whatever data it receives): I do not want to change this if at all possible
def build_granted(config):
cnx = granted_table(config)
cursor = cnx.cursor()
query = "SELECT uuid, patent_id, assignee_id, rawlocation_id, type, name_first, name_last, organization, sequence FROM rawassignee"
cursor.execute(query)
for rec in cursor:
print(rec)
Third, there is my attempt at a new function granted_table(config) which will behave like the old one but not access mysql
def granted_table(config):
return(placeholder())
class placeholder:
def cursor(self):
return conn_to_data()
class conn_to_data:
def execute(self,query): # class method
#define folder etc
print(query)
filename = "foo.tsv"
path ="/share/bar/"
file=open((path+filename))
print(file.read(100))
return file
Now, if I execute all of the above and then run
config="test"
build_granted(config)
the code breaks with
"Type Error: conn_to_data object is not iterable",
so it does not access the actual data as intended. However, if I change the line cursor.execute(query) to cursor=cursor.execute(query) it works as intended. I could do that if there is really no better solution, but it seems like there must be. I am a python newb, so maybe there even is a simple function/class that is intended for this and one of you can point it out. You might realize that the code currently always opens the same file, without actually interpreting the query. This is something I will still have to fix later, once it even works at all. It seems to me to be a somewhat messy but conceptually simple string-parsing problem, but if some of you have a smart idea, it would also be a huge help.
Thanks in advance!
For details I'm running a very recent version of python and PyODBC and am trying to remotely instantiate a backup on SQL. I don't know much about SQL. I've tried googling the issue and have tried a few tricks below including using .nextset() and trying different encodings just in case. I see a large surge on the local network of the PC with the SQL server but it very quickly tapers to nothing. I also can't seem to run a stored procedure as it will never seem to find it despite it existing and being able to run from my local machine using SSMS or the SSMS on the remote PC. Also I've given complete access to the remote account accessing the sql server as far as I know
Here is the relevant function:
def back_up_and_restore(self):
if self.Arbin not in [3, 4]:
self.errorString = "Not remote server!"
return self.errorString
self.cnxn = pyodbc.connect(self.connectionString(self.db[0]))
self.cursor = self.cnxn.cursor()
self.cnxn.autocommit = True
if self.Arbin == 3:
folder = "Fred"
else:
folder = "George"
query = r"BACKUP DATABASE [%s] TO DISK = N'\\10.130.130.5\TestLab\DATA\%s\ArbinMasterData.bak' WITH NOFORMAT, INIT, NAME = N'ArbinMasterData-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10" % ("ArbinMasterData", folder)
#query = "{CALL BackUp%s2}" % folder
query.encode(encoding='UTF-8')
print(query)
self.cursor.execute(query)
time.sleep(2)
while self.cursor.nextset():
pass
self.cnxn.close
When I run the function it just dies quickly and doesn't say anything. If I try running a stored procedure it says that the stored procedure doesn't exist. Any help would be great.
This code is working great if you need an example of PyODBC making a backup. I unfortunately was looking in the George folder instead of Fred.
I'm so confused - I've written the same code numerous times and never had this problem. My knowledge of python doesn't go far beyond self taught stuff to build a rest api with flask framework for my iOS apps. When I make my api call to retrieve some data for this specific path, MySQL keeps returning 'error' because for some reason the variable I pass is being passed as an optional type ( I think? - b/coz the print statement prints: Optional(1) )
Can someone explain what is going on here? And why this same exact code worked fine in my other paths? I've even tried casting it as a string. The function that makes the api call in my iOS app only accepts a string as parameter as well...Running the MySQL query with a hard coded value always works.
***** thwart is what i renamed MySQLdb escape_string *****
The query function:
def checkAnswers(gameId):
print("inside check answers function")
print(gameId)
id = str(gameId)
c, conn = connection()
data = c.execute("SELECT answer_one, answer_two FROM games WHERE game_id='%s'" % (thwart(id)))
if data:
return c.fetchall()
return "error"
Inside the main.py file under the correct path I have this code snippet: (print(data) always prints 'error')
print("inside check answers path")
json = request.get_json()
gameId = str(json["gameId"])
data = checkAnswers(gameId)
print("answer data ----->")
print(data)
execute has no return value, checking it, does not make any sense, just check for empty result of fetchall:
def checkAnswers(gameId):
print("inside check answers function")
print(gameId)
id = str(gameId)
c, conn = connection()
c.execute("SELECT answer_one, answer_two FROM games WHERE game_id=%s", (thwart(id),))
return c.fetchall()
This ended up being a xcode bug [Queue all iOS/ xcode insults]. Even though I was casting the variable to a string properly in xcode, it was being sent as optional...all I did was clean the project code and restart xcode and it was working as expected..... 3 hours and a headache later. what a fix....
THANKS TO ALL WHO CONTRIBUTED to this as it was basically an unanswerable question.
I am able to connect to aws-redshift with psycopg2 using python, I can query tables and get data back, etc...
However, when I try to run a create udf fucntion through psycopg2, nothing happens, no error returns but nothing gets created.
Here's my code:
def _applyFunctionToDB():
con=psycopg2.connect(dbname = redhsiftDatabase, host = redshiftHost, port = '5439', user = redshiftUser, password = redshiftPwd)
cur = con.cursor()
udf=_fileOpenWrite(udfFile)
size = os.stat(udfFile).st_size
udfCode=udf.read(size)
cur.execute(udfCode)
con.close()
I have run it through the debugger and all the pieces are there, but nothing happens when the "execute" method is invoked on the cursor.
If anyone has any advice and/or ideas on what might be going on here, please advise.
Thanks!
found answer just after posting here: Copying data from S3 to AWS redshift using python and psycopg2
I need to invoke a commit.
So, add con.commit in above code after execute.
I have a temperature sensor ([LM35][1]) interfaced with an Arduino board and my [sketch][2] is able to log values to the serial port, say /dev/ttyACM0 in Ubuntu, and I was able to install pySerial and log the temperature values into a file... I used the command
python -m serial.tools.miniterm /dev/ttyACM0 >> templogger.csv
So it will log values like
27
28
27
into the templogger.csv file.
Instead of logging into the csv file Can I log these values directily to a postresql database say Db1 with username 'abc' and password 'xyz'. Is it possible through python, Can you please help by providing the required script
Start with psycopg2, which implements the Python database API.
You'll probably want to start with simple individual INSERTs, but later on you'll be able to batch work into transactions or use COPY to improve write performance.
In general, it's better to post questions for more specific problems than this. See Stack Overflow Help. If you're stuck on something it's fine to ask for help, but it's preferable that you try to figure it out first. You'll get better results if your questions are more like: "I've tried to use psycopg2 to connect my Python program to PostgreSQL so I can write sensor logging to the database, but when I INSERT a row I get [exact error text here]. Searching for the error message hasn't helped me, so I'm a bit stuck."
In other words, try it, and if you get stuck, ask. I'd normally just close-vote a question like this as "too localized" or "not a real question", which would give you an automatic link to the question writing guide, but you're new here and you've made an effort to write a decent question so I'm trying to explain in a bit more detail.
Instead of logging into the csv file Can I log these values directily to a postresql database say Db1 with username 'abc' and password 'xyz'. Is it possible through python, Can you please help by providing the required script?
Have a good look at a SQLAlchemy tutorial, and try to make a code that can works for you. Basically, you can extrapolate from the example:
from serial import Serial
from sqlalchemy import *
class Temperature(object):
pass
class TemperatureStore:
def __init__(self, dburl):
self.db = create_engine('postgresql://%s' % dburl)
self.db.echo = True
metadata = BoundMetaData(db)
temperatures = Table('temperatures', metadata, autoload=True)
temperaturemapper = mapper(Temperature, temperatures)
self.session = create_session()
def push_temperature(self, temp):
temp = Temperature()
temp.value = temp
session.save(temp)
def main():
ser = Serial(port='/dev/ttyXXX', baudrate=115200)
tempstore = TemperatureStore('abc:xyz#localhost:5432/Db1')
while True:
tempstore.push_temperature(ser.read())
This is just an example that may or may not work (I did not test it, just wrote it in 5 minutes), so you'd better get some inspiration from that, and try to work out your own!