2

I have this connection class

import MySQLdb
import config

class DB:
    def connect(self):
        try:
            self.conn = MySQLdb.connect(config.HOST,
                                    config.USER, config.PASS, config.MYDB)
    except (AttributeError, MySQLdb.OperationalError), e:
        raise e

def query(self, sql, params = ()):
    try:
        cursor = self.conn.cursor()
        cursor.execute(sql, params)
    except (AttributeError, MySQLdb.OperationalError) as e:
        print 'exception generated during sql connection: ', e
        self.connect()
        cursor = self.conn.cursor()
        cursor.execute(sql, params)
    return cursor

def close(self):
    try:
        if self.conn:
            self.conn.close()
            print '...Closed Database Connection: ' + str(self.conn)
        else:
            print '...No Database Connection to Close.'
    except (AttributeError, MySQLdb.OperationalError) as e:
        raise e

This is the class where i am trying to pass my query

import mySql_connection



class Partner:

def __init__(self,pid):
    db=mySql_connection.DB()
    db.connect()
    Partner_data="select a.pid,a.login,a.name,a.company,a.comments,b.address,b.city,b.state,b.zip,c.email,c.phone,d.account_balance,e.account_status,f.dedupe_period,f.overall_dedupe,g.day_limit,g.month_limit from Partner a inner join Location b on a.pid=b.pid inner join Contact c on a.pid=c.pid inner join account_balance d  on a.pid=d.pid inner join account_status e on a.pid=e.pid inner join dedupe f on a.pid=f.pid inner join partner_limit g on a.pid=g.pid where a.pid='%s'",pid
    db.query(Partner_data)

    row= db.query(Partner_data).fetchall()
    logi =row[0][1] 

When i run this, i gets error saying:
TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'

When i tried this,

import mySql_connection



class Partner:

def __init__(self,pid):
    db=mySql_connection.DB()
    db.connect()
    Partner_data="select a.pid,a.login,a.name,a.company,a.comments,b.address,b.city,b.state,b.zip,c.email,c.phone,d.account_balance,e.account_status,f.dedupe_period,f.overall_dedupe,g.day_limit,g.month_limit from Partner a inner join Location b on a.pid=b.pid inner join Contact c on a.pid=c.pid inner join account_balance d  on a.pid=d.pid inner join account_status e on a.pid=e.pid inner join dedupe f on a.pid=f.pid inner join partner_limit g on a.pid=g.pid where a.pid='%s'"
    db.query(Partner_data,pid)
    row= db.query(Partner_data).fetchall()
    logi =row[0][1] 

I got this error,

_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1319''' at line 1")

1319 is the pid which i passed and got the same error with *Partner_data

its only working fine with '"+pid+'" but i dont want to do like that, can some one please help me to figure out where i am doing wrong.

Thanks in advance.

So this is how i am passing pid

Partner1= Partner("1319")
print Partner1.login
print Partner1.company
Shubham
  • 85
  • 1
  • 9

1 Answers1

0

It is just a long shot, but where do you get the "pid" from? Usually "a tuple" means, its

pid=('1319')

for example.

Can you print that value? It might also be an idea, to use

pid = str(pid)

first. Then it definitely is only a string. (Maybe not the desired one, but a string.)

Regards!

Edit: Sorry for the late understanding. You might want to try:

Partner_data="select a.pid,a.login,a.name,a.company,a.comments,b.address,b.city,b.state,b.zip,c.email,c.phone,d.account_balance,e.account_status,f.dedupe_period,f.overall_dedupe,g.day_limit,g.month_limit from Partner a inner join Location b on a.pid=b.pid inner join Contact c on a.pid=c.pid inner join account_balance d  on a.pid=d.pid inner join account_status e on a.pid=e.pid inner join dedupe f on a.pid=f.pid inner join partner_limit g on a.pid=g.pid where a.pid='%s'"%pid
Community
  • 1
  • 1
Bigfoot29
  • 70
  • 7
  • Ah now I see. What happens when you use a print("PID",type(pid),pid) before appending the pid to the Partner_data to show the results? What Python version are you using? (e.g. for 2.x, you might need to use print without the brackets) – Bigfoot29 Nov 04 '15 at 20:31
  • Can you please elaborate , i am using python 2.7.6. – Shubham Nov 04 '15 at 20:36
  • I can't help you much. I am not used to mysql dbs. I can just give you hints and try to help solving the issue on your machine itself. :/ for 2.x, the command you need to add to get the result would be: >>>print "PID",type(pid),pid <<<. Please post the result of that line before the line Partner_data here. – Bigfoot29 Nov 04 '15 at 20:49
  • So its saying String The exact output is PID – Shubham Nov 04 '15 at 20:55
  • I think I got your problem. Please see my edited answer. – Bigfoot29 Nov 04 '15 at 21:18