1

My database cannot query. when i run this code then it's show me an error .

def button_click(self):
    # shost is a QString object
    shost = self.le.text()
    if shost:

       s_h = "127.0.0.1"
       s_n = "root"
       s_p = ""
       s_d = "code"

       s_cn = mdb.connect(s_h, s_n, s_p, s_d)

       cursor = s_cn.cursor()
       today = datetime.date.today()
       mac = get_mac()
       query = "INSERT INTO `ac` (`acc`, `mac`, `date`) VALUES (%s, %s, %s)"
       re = cursor.execute(query,(shost,mac,today,))          

       if re: 
           self.ex = Example()
           self.ex.show()

       else:
          query1 = "SELECT * FROM  `ac` WHERE  `acc` = %s,`mac` = $s"
          ck = cursor.execute(query1,(shost,mac))
          if(ck):
             self.ex = Example()
             self.ex.show()
          else:
              print 'no'  

I want to know how to write sql code with python variable so how can i fix it ?

  query = "INSERT INTO `ac` (`acc`, `mac`, `date`) VALUES (%s, %s, %s)"
   re = cursor.execute(query,(shost,mac,today,))

and

          query1 = "SELECT * FROM  `ac` WHERE  `acc` = %s,`mac` = $s"
          ck = cursor.execute(query1,(shost,mac))

2 Answers 2

2

query1 = "SELECT * FROMacWHEREacc= %s,mac= $s"

$s - this is not a valid placeholder, replace it with %s.

Sign up to request clarification or add additional context in comments.

4 Comments

Nothing happen it's still error and $s is printing mistake
@alibaba What errors are you getting? You may also need to commit after the insert: mdb.commit().
This Error : Traceback (most recent call last): File "C:\Users\Tanver\Desktop\pypro\final.py", line 51, in button_click re = cursor.execute(query,(shost,mac,today,)) File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 202, in execute self.errorhandler(self, exc, value) File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.IntegrityError: (1062, "Duplicate entry 'Sabbir' for key 'PRIMARY'")
@alibaba and this is a different problem. You have a unique key constraint and you are violating it.
0
re = cursor.execute(query % (m, n, k))

1 Comment

you should add some explanation for better post quality

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.