Python variables in MySQL execute command










-1















I've looked for an answer everywhere and didn't manage to find any suitable one.



This is my code:



conn = pymysql.Connect(host="host", user="user", passwd="password", db="database")

dbhandler = conn.cursor()
table_name = today_date.split(" ")[0]
execute_it = """CREATE TABLE %s (
USERNAME CHAR(20) NOT NULL,
X CHAR(10),
Y INT,
Z INT,
A INT)"""

try:
dbhandler.execute(execute_it, table_name)
except:
print("n----------------------------nFailed to create table.")


Now I've tried to do it like this.
I tried with % separating in execute.
I tried with ? instead of %s.
I tried it with many more options and yet none of them worked for me and I failed to create the table



This is the exception I get:




(1064, "You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near ''11/14/18' (n USERNAME CHAR(20) NOT NULL, n

X CHAR(10' at line 1")




Using 5.5.52-MariaDB.



Thank you!



EDIT:



Managed to get through it.
Thanks Pavel Francírek for the help.










share|improve this question




























    -1















    I've looked for an answer everywhere and didn't manage to find any suitable one.



    This is my code:



    conn = pymysql.Connect(host="host", user="user", passwd="password", db="database")

    dbhandler = conn.cursor()
    table_name = today_date.split(" ")[0]
    execute_it = """CREATE TABLE %s (
    USERNAME CHAR(20) NOT NULL,
    X CHAR(10),
    Y INT,
    Z INT,
    A INT)"""

    try:
    dbhandler.execute(execute_it, table_name)
    except:
    print("n----------------------------nFailed to create table.")


    Now I've tried to do it like this.
    I tried with % separating in execute.
    I tried with ? instead of %s.
    I tried it with many more options and yet none of them worked for me and I failed to create the table



    This is the exception I get:




    (1064, "You have an error in your SQL syntax; check the manual that
    corresponds to your MariaDB server version for the right syntax to use
    near ''11/14/18' (n USERNAME CHAR(20) NOT NULL, n

    X CHAR(10' at line 1")




    Using 5.5.52-MariaDB.



    Thank you!



    EDIT:



    Managed to get through it.
    Thanks Pavel Francírek for the help.










    share|improve this question


























      -1












      -1








      -1








      I've looked for an answer everywhere and didn't manage to find any suitable one.



      This is my code:



      conn = pymysql.Connect(host="host", user="user", passwd="password", db="database")

      dbhandler = conn.cursor()
      table_name = today_date.split(" ")[0]
      execute_it = """CREATE TABLE %s (
      USERNAME CHAR(20) NOT NULL,
      X CHAR(10),
      Y INT,
      Z INT,
      A INT)"""

      try:
      dbhandler.execute(execute_it, table_name)
      except:
      print("n----------------------------nFailed to create table.")


      Now I've tried to do it like this.
      I tried with % separating in execute.
      I tried with ? instead of %s.
      I tried it with many more options and yet none of them worked for me and I failed to create the table



      This is the exception I get:




      (1064, "You have an error in your SQL syntax; check the manual that
      corresponds to your MariaDB server version for the right syntax to use
      near ''11/14/18' (n USERNAME CHAR(20) NOT NULL, n

      X CHAR(10' at line 1")




      Using 5.5.52-MariaDB.



      Thank you!



      EDIT:



      Managed to get through it.
      Thanks Pavel Francírek for the help.










      share|improve this question
















      I've looked for an answer everywhere and didn't manage to find any suitable one.



      This is my code:



      conn = pymysql.Connect(host="host", user="user", passwd="password", db="database")

      dbhandler = conn.cursor()
      table_name = today_date.split(" ")[0]
      execute_it = """CREATE TABLE %s (
      USERNAME CHAR(20) NOT NULL,
      X CHAR(10),
      Y INT,
      Z INT,
      A INT)"""

      try:
      dbhandler.execute(execute_it, table_name)
      except:
      print("n----------------------------nFailed to create table.")


      Now I've tried to do it like this.
      I tried with % separating in execute.
      I tried with ? instead of %s.
      I tried it with many more options and yet none of them worked for me and I failed to create the table



      This is the exception I get:




      (1064, "You have an error in your SQL syntax; check the manual that
      corresponds to your MariaDB server version for the right syntax to use
      near ''11/14/18' (n USERNAME CHAR(20) NOT NULL, n

      X CHAR(10' at line 1")




      Using 5.5.52-MariaDB.



      Thank you!



      EDIT:



      Managed to get through it.
      Thanks Pavel Francírek for the help.







      python mysql mysql-python mysql-error-1064






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 15:59







      yakir saadia

















      asked Nov 14 '18 at 13:28









      yakir saadiayakir saadia

      235




      235






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Problem is not in placeholder, but in date format. Character "/" is not allowed in table name. Try something like:



          table_name = today_date.split(" ")[0].replace("/","")


          I assume that all numbers in your date format are 2-digit.






          share|improve this answer























          • Wrong. same error....

            – yakir saadia
            Nov 14 '18 at 13:46











          • By the way I also tried it with """INSERT INTO table_name (USERNAME, X,Y, Z, A) VALUES (%s, %s, %d, %d, %d)""", (value1, value2, value3, value4, value5)

            – yakir saadia
            Nov 14 '18 at 13:46












          • Well, maybe table name must start with letter: table_name = "t"+today_date.split(" ")[0].replace("/","")

            – Pavel Francírek
            Nov 14 '18 at 13:47












          • Usually, there is only one placeholder (%s) and not more like in string format (no %d etc.)

            – Pavel Francírek
            Nov 14 '18 at 13:49











          • See the second comment but anyways I tried also what you've said and it didn't work. I'm actually losing my mind here, I tried almost every option and nothing worked...

            – yakir saadia
            Nov 14 '18 at 13:50










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301358%2fpython-variables-in-mysql-execute-command%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Problem is not in placeholder, but in date format. Character "/" is not allowed in table name. Try something like:



          table_name = today_date.split(" ")[0].replace("/","")


          I assume that all numbers in your date format are 2-digit.






          share|improve this answer























          • Wrong. same error....

            – yakir saadia
            Nov 14 '18 at 13:46











          • By the way I also tried it with """INSERT INTO table_name (USERNAME, X,Y, Z, A) VALUES (%s, %s, %d, %d, %d)""", (value1, value2, value3, value4, value5)

            – yakir saadia
            Nov 14 '18 at 13:46












          • Well, maybe table name must start with letter: table_name = "t"+today_date.split(" ")[0].replace("/","")

            – Pavel Francírek
            Nov 14 '18 at 13:47












          • Usually, there is only one placeholder (%s) and not more like in string format (no %d etc.)

            – Pavel Francírek
            Nov 14 '18 at 13:49











          • See the second comment but anyways I tried also what you've said and it didn't work. I'm actually losing my mind here, I tried almost every option and nothing worked...

            – yakir saadia
            Nov 14 '18 at 13:50















          0














          Problem is not in placeholder, but in date format. Character "/" is not allowed in table name. Try something like:



          table_name = today_date.split(" ")[0].replace("/","")


          I assume that all numbers in your date format are 2-digit.






          share|improve this answer























          • Wrong. same error....

            – yakir saadia
            Nov 14 '18 at 13:46











          • By the way I also tried it with """INSERT INTO table_name (USERNAME, X,Y, Z, A) VALUES (%s, %s, %d, %d, %d)""", (value1, value2, value3, value4, value5)

            – yakir saadia
            Nov 14 '18 at 13:46












          • Well, maybe table name must start with letter: table_name = "t"+today_date.split(" ")[0].replace("/","")

            – Pavel Francírek
            Nov 14 '18 at 13:47












          • Usually, there is only one placeholder (%s) and not more like in string format (no %d etc.)

            – Pavel Francírek
            Nov 14 '18 at 13:49











          • See the second comment but anyways I tried also what you've said and it didn't work. I'm actually losing my mind here, I tried almost every option and nothing worked...

            – yakir saadia
            Nov 14 '18 at 13:50













          0












          0








          0







          Problem is not in placeholder, but in date format. Character "/" is not allowed in table name. Try something like:



          table_name = today_date.split(" ")[0].replace("/","")


          I assume that all numbers in your date format are 2-digit.






          share|improve this answer













          Problem is not in placeholder, but in date format. Character "/" is not allowed in table name. Try something like:



          table_name = today_date.split(" ")[0].replace("/","")


          I assume that all numbers in your date format are 2-digit.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 13:45









          Pavel FrancírekPavel Francírek

          8316




          8316












          • Wrong. same error....

            – yakir saadia
            Nov 14 '18 at 13:46











          • By the way I also tried it with """INSERT INTO table_name (USERNAME, X,Y, Z, A) VALUES (%s, %s, %d, %d, %d)""", (value1, value2, value3, value4, value5)

            – yakir saadia
            Nov 14 '18 at 13:46












          • Well, maybe table name must start with letter: table_name = "t"+today_date.split(" ")[0].replace("/","")

            – Pavel Francírek
            Nov 14 '18 at 13:47












          • Usually, there is only one placeholder (%s) and not more like in string format (no %d etc.)

            – Pavel Francírek
            Nov 14 '18 at 13:49











          • See the second comment but anyways I tried also what you've said and it didn't work. I'm actually losing my mind here, I tried almost every option and nothing worked...

            – yakir saadia
            Nov 14 '18 at 13:50

















          • Wrong. same error....

            – yakir saadia
            Nov 14 '18 at 13:46











          • By the way I also tried it with """INSERT INTO table_name (USERNAME, X,Y, Z, A) VALUES (%s, %s, %d, %d, %d)""", (value1, value2, value3, value4, value5)

            – yakir saadia
            Nov 14 '18 at 13:46












          • Well, maybe table name must start with letter: table_name = "t"+today_date.split(" ")[0].replace("/","")

            – Pavel Francírek
            Nov 14 '18 at 13:47












          • Usually, there is only one placeholder (%s) and not more like in string format (no %d etc.)

            – Pavel Francírek
            Nov 14 '18 at 13:49











          • See the second comment but anyways I tried also what you've said and it didn't work. I'm actually losing my mind here, I tried almost every option and nothing worked...

            – yakir saadia
            Nov 14 '18 at 13:50
















          Wrong. same error....

          – yakir saadia
          Nov 14 '18 at 13:46





          Wrong. same error....

          – yakir saadia
          Nov 14 '18 at 13:46













          By the way I also tried it with """INSERT INTO table_name (USERNAME, X,Y, Z, A) VALUES (%s, %s, %d, %d, %d)""", (value1, value2, value3, value4, value5)

          – yakir saadia
          Nov 14 '18 at 13:46






          By the way I also tried it with """INSERT INTO table_name (USERNAME, X,Y, Z, A) VALUES (%s, %s, %d, %d, %d)""", (value1, value2, value3, value4, value5)

          – yakir saadia
          Nov 14 '18 at 13:46














          Well, maybe table name must start with letter: table_name = "t"+today_date.split(" ")[0].replace("/","")

          – Pavel Francírek
          Nov 14 '18 at 13:47






          Well, maybe table name must start with letter: table_name = "t"+today_date.split(" ")[0].replace("/","")

          – Pavel Francírek
          Nov 14 '18 at 13:47














          Usually, there is only one placeholder (%s) and not more like in string format (no %d etc.)

          – Pavel Francírek
          Nov 14 '18 at 13:49





          Usually, there is only one placeholder (%s) and not more like in string format (no %d etc.)

          – Pavel Francírek
          Nov 14 '18 at 13:49













          See the second comment but anyways I tried also what you've said and it didn't work. I'm actually losing my mind here, I tried almost every option and nothing worked...

          – yakir saadia
          Nov 14 '18 at 13:50





          See the second comment but anyways I tried also what you've said and it didn't work. I'm actually losing my mind here, I tried almost every option and nothing worked...

          – yakir saadia
          Nov 14 '18 at 13:50

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301358%2fpython-variables-in-mysql-execute-command%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          Evgeni Malkin