MySQL/Python connector not being able to search [duplicate]












-1
















This question already has an answer here:




  • MySQL/Python -> Wrong Syntax for Placeholder in Statements?

    1 answer




import mysql.connector

config = {
'user': 'root',
'password': '*******',
'host': '127.0.0.1',
'database': 'mydb',
'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()

find_user = ("SELECT * FROM HM_Login WHERE Username = '%s' ")
data_Pupil = {
'Username': "GJM"
}
cursor.execute(find_user, data_Pupil)

lit = cursor.fetchall()
print(lit)
cursor.close()
cnx.close()


I have a database that works and i am having a problem trying to search the database and pull one row of one column when i was inserting into the database the %S worked just fine but now it only works if i have a value inside the the query. this is using the mysql connector for python.



mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '' at line 1


I am getting this error which is extremely insightful and not helpful at all if there is anything you can do to help it would mean a lot.










share|improve this question













marked as duplicate by deceze mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 12:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    -1
















    This question already has an answer here:




    • MySQL/Python -> Wrong Syntax for Placeholder in Statements?

      1 answer




    import mysql.connector

    config = {
    'user': 'root',
    'password': '*******',
    'host': '127.0.0.1',
    'database': 'mydb',
    'raise_on_warnings': True
    }

    cnx = mysql.connector.connect(**config)
    cursor = cnx.cursor()

    find_user = ("SELECT * FROM HM_Login WHERE Username = '%s' ")
    data_Pupil = {
    'Username': "GJM"
    }
    cursor.execute(find_user, data_Pupil)

    lit = cursor.fetchall()
    print(lit)
    cursor.close()
    cnx.close()


    I have a database that works and i am having a problem trying to search the database and pull one row of one column when i was inserting into the database the %S worked just fine but now it only works if i have a value inside the the query. this is using the mysql connector for python.



    mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '' at line 1


    I am getting this error which is extremely insightful and not helpful at all if there is anything you can do to help it would mean a lot.










    share|improve this question













    marked as duplicate by deceze mysql
    Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 26 '18 at 12:31


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      -1












      -1








      -1









      This question already has an answer here:




      • MySQL/Python -> Wrong Syntax for Placeholder in Statements?

        1 answer




      import mysql.connector

      config = {
      'user': 'root',
      'password': '*******',
      'host': '127.0.0.1',
      'database': 'mydb',
      'raise_on_warnings': True
      }

      cnx = mysql.connector.connect(**config)
      cursor = cnx.cursor()

      find_user = ("SELECT * FROM HM_Login WHERE Username = '%s' ")
      data_Pupil = {
      'Username': "GJM"
      }
      cursor.execute(find_user, data_Pupil)

      lit = cursor.fetchall()
      print(lit)
      cursor.close()
      cnx.close()


      I have a database that works and i am having a problem trying to search the database and pull one row of one column when i was inserting into the database the %S worked just fine but now it only works if i have a value inside the the query. this is using the mysql connector for python.



      mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '' at line 1


      I am getting this error which is extremely insightful and not helpful at all if there is anything you can do to help it would mean a lot.










      share|improve this question















      This question already has an answer here:




      • MySQL/Python -> Wrong Syntax for Placeholder in Statements?

        1 answer




      import mysql.connector

      config = {
      'user': 'root',
      'password': '*******',
      'host': '127.0.0.1',
      'database': 'mydb',
      'raise_on_warnings': True
      }

      cnx = mysql.connector.connect(**config)
      cursor = cnx.cursor()

      find_user = ("SELECT * FROM HM_Login WHERE Username = '%s' ")
      data_Pupil = {
      'Username': "GJM"
      }
      cursor.execute(find_user, data_Pupil)

      lit = cursor.fetchall()
      print(lit)
      cursor.close()
      cnx.close()


      I have a database that works and i am having a problem trying to search the database and pull one row of one column when i was inserting into the database the %S worked just fine but now it only works if i have a value inside the the query. this is using the mysql connector for python.



      mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '' at line 1


      I am getting this error which is extremely insightful and not helpful at all if there is anything you can do to help it would mean a lot.





      This question already has an answer here:




      • MySQL/Python -> Wrong Syntax for Placeholder in Statements?

        1 answer








      python mysql database






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 12:25









      Zach Creagh-CoenZach Creagh-Coen

      33




      33




      marked as duplicate by deceze mysql
      Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 26 '18 at 12:31


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by deceze mysql
      Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 26 '18 at 12:31


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          0














          As the error statement says. You have an SQL error. You are trying to input a variable as a positional parameter but you've used a dictionary on a variables 'place'.



          Instead you should use %s for variables and tuples with variables and then do the following:



          find_user = "SELECT * FROM HM_Login WHERE Username = %s"
          data_Pupil = ('GJM',)
          cursor.execute(find_user, data_Pupil)


          It is also possible to use dictionary - but you shouldn't. Despite that I'm still going to show it here as I had to dig into the explanation to understand why.



          find_user = "SELECT * FROM HM_Login WHERE Username = '{Username}'".format(**data_Pupil)
          data_Pupil = {
          'Username': "GJM"
          }


          The above opens up for sql-injections, as I was told per the comments - and here is why; Say we have a username that is identical to the following:



          username = "'MR SQL Injection');DROP TABLE HM_Login;"


          That would result in an SQL Query that drops the table.



          SELECT * FROM HM_Login WHERE Username = 'MR SQL Injection');DROP TABLE HM_Login;


          To avoid sql-injection as above. Use the first solution






          share|improve this answer


























          • that has worked thank you very much

            – Zach Creagh-Coen
            Nov 26 '18 at 12:32











          • Even though they look similar, MySQL statement placeholders and Python string format are nowhere near interchangeable.

            – deceze
            Nov 26 '18 at 12:33











          • @Zach Don't use this, you're opening yourself up to SQL injection!

            – deceze
            Nov 26 '18 at 12:33











          • then how do i fix this in a way that works as ? does not work with what i am doing

            – Zach Creagh-Coen
            Nov 26 '18 at 12:33











          • @Zach stackoverflow.com/a/49927569/476

            – deceze
            Nov 26 '18 at 12:34



















          0














          Your placeholder syntax is for positional parameters but you've used a dictionary. Replace that with a tuple:



          find_user = ("SELECT * FROM HM_Login WHERE Username = %s")
          data_pupil = ('GJM',)
          cursor.execute(find_user, data_Pupil)





          share|improve this answer


























          • i then get this error ? mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'GJM''' at line 1

            – Zach Creagh-Coen
            Nov 26 '18 at 12:28













          • You shouldn't surround your placeholder with quotes, the whole point of placeholders is that they do that for you. Updated answer with full code.

            – Daniel Roseman
            Nov 26 '18 at 13:04


















          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          As the error statement says. You have an SQL error. You are trying to input a variable as a positional parameter but you've used a dictionary on a variables 'place'.



          Instead you should use %s for variables and tuples with variables and then do the following:



          find_user = "SELECT * FROM HM_Login WHERE Username = %s"
          data_Pupil = ('GJM',)
          cursor.execute(find_user, data_Pupil)


          It is also possible to use dictionary - but you shouldn't. Despite that I'm still going to show it here as I had to dig into the explanation to understand why.



          find_user = "SELECT * FROM HM_Login WHERE Username = '{Username}'".format(**data_Pupil)
          data_Pupil = {
          'Username': "GJM"
          }


          The above opens up for sql-injections, as I was told per the comments - and here is why; Say we have a username that is identical to the following:



          username = "'MR SQL Injection');DROP TABLE HM_Login;"


          That would result in an SQL Query that drops the table.



          SELECT * FROM HM_Login WHERE Username = 'MR SQL Injection');DROP TABLE HM_Login;


          To avoid sql-injection as above. Use the first solution






          share|improve this answer


























          • that has worked thank you very much

            – Zach Creagh-Coen
            Nov 26 '18 at 12:32











          • Even though they look similar, MySQL statement placeholders and Python string format are nowhere near interchangeable.

            – deceze
            Nov 26 '18 at 12:33











          • @Zach Don't use this, you're opening yourself up to SQL injection!

            – deceze
            Nov 26 '18 at 12:33











          • then how do i fix this in a way that works as ? does not work with what i am doing

            – Zach Creagh-Coen
            Nov 26 '18 at 12:33











          • @Zach stackoverflow.com/a/49927569/476

            – deceze
            Nov 26 '18 at 12:34
















          0














          As the error statement says. You have an SQL error. You are trying to input a variable as a positional parameter but you've used a dictionary on a variables 'place'.



          Instead you should use %s for variables and tuples with variables and then do the following:



          find_user = "SELECT * FROM HM_Login WHERE Username = %s"
          data_Pupil = ('GJM',)
          cursor.execute(find_user, data_Pupil)


          It is also possible to use dictionary - but you shouldn't. Despite that I'm still going to show it here as I had to dig into the explanation to understand why.



          find_user = "SELECT * FROM HM_Login WHERE Username = '{Username}'".format(**data_Pupil)
          data_Pupil = {
          'Username': "GJM"
          }


          The above opens up for sql-injections, as I was told per the comments - and here is why; Say we have a username that is identical to the following:



          username = "'MR SQL Injection');DROP TABLE HM_Login;"


          That would result in an SQL Query that drops the table.



          SELECT * FROM HM_Login WHERE Username = 'MR SQL Injection');DROP TABLE HM_Login;


          To avoid sql-injection as above. Use the first solution






          share|improve this answer


























          • that has worked thank you very much

            – Zach Creagh-Coen
            Nov 26 '18 at 12:32











          • Even though they look similar, MySQL statement placeholders and Python string format are nowhere near interchangeable.

            – deceze
            Nov 26 '18 at 12:33











          • @Zach Don't use this, you're opening yourself up to SQL injection!

            – deceze
            Nov 26 '18 at 12:33











          • then how do i fix this in a way that works as ? does not work with what i am doing

            – Zach Creagh-Coen
            Nov 26 '18 at 12:33











          • @Zach stackoverflow.com/a/49927569/476

            – deceze
            Nov 26 '18 at 12:34














          0












          0








          0







          As the error statement says. You have an SQL error. You are trying to input a variable as a positional parameter but you've used a dictionary on a variables 'place'.



          Instead you should use %s for variables and tuples with variables and then do the following:



          find_user = "SELECT * FROM HM_Login WHERE Username = %s"
          data_Pupil = ('GJM',)
          cursor.execute(find_user, data_Pupil)


          It is also possible to use dictionary - but you shouldn't. Despite that I'm still going to show it here as I had to dig into the explanation to understand why.



          find_user = "SELECT * FROM HM_Login WHERE Username = '{Username}'".format(**data_Pupil)
          data_Pupil = {
          'Username': "GJM"
          }


          The above opens up for sql-injections, as I was told per the comments - and here is why; Say we have a username that is identical to the following:



          username = "'MR SQL Injection');DROP TABLE HM_Login;"


          That would result in an SQL Query that drops the table.



          SELECT * FROM HM_Login WHERE Username = 'MR SQL Injection');DROP TABLE HM_Login;


          To avoid sql-injection as above. Use the first solution






          share|improve this answer















          As the error statement says. You have an SQL error. You are trying to input a variable as a positional parameter but you've used a dictionary on a variables 'place'.



          Instead you should use %s for variables and tuples with variables and then do the following:



          find_user = "SELECT * FROM HM_Login WHERE Username = %s"
          data_Pupil = ('GJM',)
          cursor.execute(find_user, data_Pupil)


          It is also possible to use dictionary - but you shouldn't. Despite that I'm still going to show it here as I had to dig into the explanation to understand why.



          find_user = "SELECT * FROM HM_Login WHERE Username = '{Username}'".format(**data_Pupil)
          data_Pupil = {
          'Username': "GJM"
          }


          The above opens up for sql-injections, as I was told per the comments - and here is why; Say we have a username that is identical to the following:



          username = "'MR SQL Injection');DROP TABLE HM_Login;"


          That would result in an SQL Query that drops the table.



          SELECT * FROM HM_Login WHERE Username = 'MR SQL Injection');DROP TABLE HM_Login;


          To avoid sql-injection as above. Use the first solution







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 13:25

























          answered Nov 26 '18 at 12:30









          PhilipPhilip

          336313




          336313













          • that has worked thank you very much

            – Zach Creagh-Coen
            Nov 26 '18 at 12:32











          • Even though they look similar, MySQL statement placeholders and Python string format are nowhere near interchangeable.

            – deceze
            Nov 26 '18 at 12:33











          • @Zach Don't use this, you're opening yourself up to SQL injection!

            – deceze
            Nov 26 '18 at 12:33











          • then how do i fix this in a way that works as ? does not work with what i am doing

            – Zach Creagh-Coen
            Nov 26 '18 at 12:33











          • @Zach stackoverflow.com/a/49927569/476

            – deceze
            Nov 26 '18 at 12:34



















          • that has worked thank you very much

            – Zach Creagh-Coen
            Nov 26 '18 at 12:32











          • Even though they look similar, MySQL statement placeholders and Python string format are nowhere near interchangeable.

            – deceze
            Nov 26 '18 at 12:33











          • @Zach Don't use this, you're opening yourself up to SQL injection!

            – deceze
            Nov 26 '18 at 12:33











          • then how do i fix this in a way that works as ? does not work with what i am doing

            – Zach Creagh-Coen
            Nov 26 '18 at 12:33











          • @Zach stackoverflow.com/a/49927569/476

            – deceze
            Nov 26 '18 at 12:34

















          that has worked thank you very much

          – Zach Creagh-Coen
          Nov 26 '18 at 12:32





          that has worked thank you very much

          – Zach Creagh-Coen
          Nov 26 '18 at 12:32













          Even though they look similar, MySQL statement placeholders and Python string format are nowhere near interchangeable.

          – deceze
          Nov 26 '18 at 12:33





          Even though they look similar, MySQL statement placeholders and Python string format are nowhere near interchangeable.

          – deceze
          Nov 26 '18 at 12:33













          @Zach Don't use this, you're opening yourself up to SQL injection!

          – deceze
          Nov 26 '18 at 12:33





          @Zach Don't use this, you're opening yourself up to SQL injection!

          – deceze
          Nov 26 '18 at 12:33













          then how do i fix this in a way that works as ? does not work with what i am doing

          – Zach Creagh-Coen
          Nov 26 '18 at 12:33





          then how do i fix this in a way that works as ? does not work with what i am doing

          – Zach Creagh-Coen
          Nov 26 '18 at 12:33













          @Zach stackoverflow.com/a/49927569/476

          – deceze
          Nov 26 '18 at 12:34





          @Zach stackoverflow.com/a/49927569/476

          – deceze
          Nov 26 '18 at 12:34













          0














          Your placeholder syntax is for positional parameters but you've used a dictionary. Replace that with a tuple:



          find_user = ("SELECT * FROM HM_Login WHERE Username = %s")
          data_pupil = ('GJM',)
          cursor.execute(find_user, data_Pupil)





          share|improve this answer


























          • i then get this error ? mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'GJM''' at line 1

            – Zach Creagh-Coen
            Nov 26 '18 at 12:28













          • You shouldn't surround your placeholder with quotes, the whole point of placeholders is that they do that for you. Updated answer with full code.

            – Daniel Roseman
            Nov 26 '18 at 13:04
















          0














          Your placeholder syntax is for positional parameters but you've used a dictionary. Replace that with a tuple:



          find_user = ("SELECT * FROM HM_Login WHERE Username = %s")
          data_pupil = ('GJM',)
          cursor.execute(find_user, data_Pupil)





          share|improve this answer


























          • i then get this error ? mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'GJM''' at line 1

            – Zach Creagh-Coen
            Nov 26 '18 at 12:28













          • You shouldn't surround your placeholder with quotes, the whole point of placeholders is that they do that for you. Updated answer with full code.

            – Daniel Roseman
            Nov 26 '18 at 13:04














          0












          0








          0







          Your placeholder syntax is for positional parameters but you've used a dictionary. Replace that with a tuple:



          find_user = ("SELECT * FROM HM_Login WHERE Username = %s")
          data_pupil = ('GJM',)
          cursor.execute(find_user, data_Pupil)





          share|improve this answer















          Your placeholder syntax is for positional parameters but you've used a dictionary. Replace that with a tuple:



          find_user = ("SELECT * FROM HM_Login WHERE Username = %s")
          data_pupil = ('GJM',)
          cursor.execute(find_user, data_Pupil)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 13:04

























          answered Nov 26 '18 at 12:26









          Daniel RosemanDaniel Roseman

          458k42594653




          458k42594653













          • i then get this error ? mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'GJM''' at line 1

            – Zach Creagh-Coen
            Nov 26 '18 at 12:28













          • You shouldn't surround your placeholder with quotes, the whole point of placeholders is that they do that for you. Updated answer with full code.

            – Daniel Roseman
            Nov 26 '18 at 13:04



















          • i then get this error ? mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'GJM''' at line 1

            – Zach Creagh-Coen
            Nov 26 '18 at 12:28













          • You shouldn't surround your placeholder with quotes, the whole point of placeholders is that they do that for you. Updated answer with full code.

            – Daniel Roseman
            Nov 26 '18 at 13:04

















          i then get this error ? mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'GJM''' at line 1

          – Zach Creagh-Coen
          Nov 26 '18 at 12:28







          i then get this error ? mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'GJM''' at line 1

          – Zach Creagh-Coen
          Nov 26 '18 at 12:28















          You shouldn't surround your placeholder with quotes, the whole point of placeholders is that they do that for you. Updated answer with full code.

          – Daniel Roseman
          Nov 26 '18 at 13:04





          You shouldn't surround your placeholder with quotes, the whole point of placeholders is that they do that for you. Updated answer with full code.

          – Daniel Roseman
          Nov 26 '18 at 13:04



          Popular posts from this blog

          Ottavio Pratesi

          Tricia Helfer

          15 giugno