Python DELETE mysql/mariadb with a python variable not working












0















I am trying to do something as described in the post below, I have simplified this with a basic python program, not using flask, just to prove my concept, but I'm having some issues.



flask_mysqldb Delete FROM variable table



My code is as follows;



import mysql.connector as mariadb

new = 'ksk'
mariadb_connection = mariadb.connect(user='root', password='mypwd',
database='customers')
cursor = mariadb_connection.cursor()

cursor.execute("DELETE FROM customer_info WHERE name = %s" % (new))

mariadb_connection.commit()
print('its gone')


this throws up an error saying in my table,



"unknown column 'ksk' in where clause" 


this isn't true I can see a row where the name is ksk.



Furthermore to prove this, if I change my delete statement to:



cursor.execute("DELETE FROM customer_info WHERE name = 'ksk'


the row ksk gets successfully deleted. Where am I going wrong?










share|improve this question

























  • Short answer: Use comma instead of percent.

    – Rick James
    Nov 15 '18 at 22:30











  • Tried this, it throws up an error that a string is not callable. The comma method works when using the SELECT feature not in the DELETE feature for some reason?

    – PrimitiveSource
    Nov 15 '18 at 22:46











  • Your string is not being escaped/quoted. It's treated like a column name.

    – Garr Godfrey
    Nov 15 '18 at 23:01













  • Please could you show me how to rectify that? Is putting the new = 'ksk' not suitable? Or do I need to quote as 'new' in my DELETE statement?

    – PrimitiveSource
    Nov 15 '18 at 23:09











  • @PrimitiveSource - Since you have only one item, you need an extra comma: (new,) to make it a "tuple".

    – Rick James
    Nov 15 '18 at 23:45


















0















I am trying to do something as described in the post below, I have simplified this with a basic python program, not using flask, just to prove my concept, but I'm having some issues.



flask_mysqldb Delete FROM variable table



My code is as follows;



import mysql.connector as mariadb

new = 'ksk'
mariadb_connection = mariadb.connect(user='root', password='mypwd',
database='customers')
cursor = mariadb_connection.cursor()

cursor.execute("DELETE FROM customer_info WHERE name = %s" % (new))

mariadb_connection.commit()
print('its gone')


this throws up an error saying in my table,



"unknown column 'ksk' in where clause" 


this isn't true I can see a row where the name is ksk.



Furthermore to prove this, if I change my delete statement to:



cursor.execute("DELETE FROM customer_info WHERE name = 'ksk'


the row ksk gets successfully deleted. Where am I going wrong?










share|improve this question

























  • Short answer: Use comma instead of percent.

    – Rick James
    Nov 15 '18 at 22:30











  • Tried this, it throws up an error that a string is not callable. The comma method works when using the SELECT feature not in the DELETE feature for some reason?

    – PrimitiveSource
    Nov 15 '18 at 22:46











  • Your string is not being escaped/quoted. It's treated like a column name.

    – Garr Godfrey
    Nov 15 '18 at 23:01













  • Please could you show me how to rectify that? Is putting the new = 'ksk' not suitable? Or do I need to quote as 'new' in my DELETE statement?

    – PrimitiveSource
    Nov 15 '18 at 23:09











  • @PrimitiveSource - Since you have only one item, you need an extra comma: (new,) to make it a "tuple".

    – Rick James
    Nov 15 '18 at 23:45
















0












0








0








I am trying to do something as described in the post below, I have simplified this with a basic python program, not using flask, just to prove my concept, but I'm having some issues.



flask_mysqldb Delete FROM variable table



My code is as follows;



import mysql.connector as mariadb

new = 'ksk'
mariadb_connection = mariadb.connect(user='root', password='mypwd',
database='customers')
cursor = mariadb_connection.cursor()

cursor.execute("DELETE FROM customer_info WHERE name = %s" % (new))

mariadb_connection.commit()
print('its gone')


this throws up an error saying in my table,



"unknown column 'ksk' in where clause" 


this isn't true I can see a row where the name is ksk.



Furthermore to prove this, if I change my delete statement to:



cursor.execute("DELETE FROM customer_info WHERE name = 'ksk'


the row ksk gets successfully deleted. Where am I going wrong?










share|improve this question
















I am trying to do something as described in the post below, I have simplified this with a basic python program, not using flask, just to prove my concept, but I'm having some issues.



flask_mysqldb Delete FROM variable table



My code is as follows;



import mysql.connector as mariadb

new = 'ksk'
mariadb_connection = mariadb.connect(user='root', password='mypwd',
database='customers')
cursor = mariadb_connection.cursor()

cursor.execute("DELETE FROM customer_info WHERE name = %s" % (new))

mariadb_connection.commit()
print('its gone')


this throws up an error saying in my table,



"unknown column 'ksk' in where clause" 


this isn't true I can see a row where the name is ksk.



Furthermore to prove this, if I change my delete statement to:



cursor.execute("DELETE FROM customer_info WHERE name = 'ksk'


the row ksk gets successfully deleted. Where am I going wrong?







python mysql mariadb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 15 '18 at 16:03









Cœur

19.1k9114155




19.1k9114155










asked Nov 15 '18 at 22:26









PrimitiveSourcePrimitiveSource

226




226













  • Short answer: Use comma instead of percent.

    – Rick James
    Nov 15 '18 at 22:30











  • Tried this, it throws up an error that a string is not callable. The comma method works when using the SELECT feature not in the DELETE feature for some reason?

    – PrimitiveSource
    Nov 15 '18 at 22:46











  • Your string is not being escaped/quoted. It's treated like a column name.

    – Garr Godfrey
    Nov 15 '18 at 23:01













  • Please could you show me how to rectify that? Is putting the new = 'ksk' not suitable? Or do I need to quote as 'new' in my DELETE statement?

    – PrimitiveSource
    Nov 15 '18 at 23:09











  • @PrimitiveSource - Since you have only one item, you need an extra comma: (new,) to make it a "tuple".

    – Rick James
    Nov 15 '18 at 23:45





















  • Short answer: Use comma instead of percent.

    – Rick James
    Nov 15 '18 at 22:30











  • Tried this, it throws up an error that a string is not callable. The comma method works when using the SELECT feature not in the DELETE feature for some reason?

    – PrimitiveSource
    Nov 15 '18 at 22:46











  • Your string is not being escaped/quoted. It's treated like a column name.

    – Garr Godfrey
    Nov 15 '18 at 23:01













  • Please could you show me how to rectify that? Is putting the new = 'ksk' not suitable? Or do I need to quote as 'new' in my DELETE statement?

    – PrimitiveSource
    Nov 15 '18 at 23:09











  • @PrimitiveSource - Since you have only one item, you need an extra comma: (new,) to make it a "tuple".

    – Rick James
    Nov 15 '18 at 23:45



















Short answer: Use comma instead of percent.

– Rick James
Nov 15 '18 at 22:30





Short answer: Use comma instead of percent.

– Rick James
Nov 15 '18 at 22:30













Tried this, it throws up an error that a string is not callable. The comma method works when using the SELECT feature not in the DELETE feature for some reason?

– PrimitiveSource
Nov 15 '18 at 22:46





Tried this, it throws up an error that a string is not callable. The comma method works when using the SELECT feature not in the DELETE feature for some reason?

– PrimitiveSource
Nov 15 '18 at 22:46













Your string is not being escaped/quoted. It's treated like a column name.

– Garr Godfrey
Nov 15 '18 at 23:01







Your string is not being escaped/quoted. It's treated like a column name.

– Garr Godfrey
Nov 15 '18 at 23:01















Please could you show me how to rectify that? Is putting the new = 'ksk' not suitable? Or do I need to quote as 'new' in my DELETE statement?

– PrimitiveSource
Nov 15 '18 at 23:09





Please could you show me how to rectify that? Is putting the new = 'ksk' not suitable? Or do I need to quote as 'new' in my DELETE statement?

– PrimitiveSource
Nov 15 '18 at 23:09













@PrimitiveSource - Since you have only one item, you need an extra comma: (new,) to make it a "tuple".

– Rick James
Nov 15 '18 at 23:45







@PrimitiveSource - Since you have only one item, you need an extra comma: (new,) to make it a "tuple".

– Rick James
Nov 15 '18 at 23:45














1 Answer
1






active

oldest

votes


















0














https://pynative.com/python-mysql-execute-parameterized-query-using-prepared-statement/ --




We then added those two columns value in the input tuple in sequential order and passed SQL update query and input tuple t0 cursor.execute() function, remember tuple contain user data in sequential order of placeholders.



The CURSOR’s execute() method takes an optional second argument containing a list of data values. each entry in the list must be in tuple format to associate with parameter markers in the statement.







share|improve this answer























    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%2f53328746%2fpython-delete-mysql-mariadb-with-a-python-variable-not-working%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














    https://pynative.com/python-mysql-execute-parameterized-query-using-prepared-statement/ --




    We then added those two columns value in the input tuple in sequential order and passed SQL update query and input tuple t0 cursor.execute() function, remember tuple contain user data in sequential order of placeholders.



    The CURSOR’s execute() method takes an optional second argument containing a list of data values. each entry in the list must be in tuple format to associate with parameter markers in the statement.







    share|improve this answer




























      0














      https://pynative.com/python-mysql-execute-parameterized-query-using-prepared-statement/ --




      We then added those two columns value in the input tuple in sequential order and passed SQL update query and input tuple t0 cursor.execute() function, remember tuple contain user data in sequential order of placeholders.



      The CURSOR’s execute() method takes an optional second argument containing a list of data values. each entry in the list must be in tuple format to associate with parameter markers in the statement.







      share|improve this answer


























        0












        0








        0







        https://pynative.com/python-mysql-execute-parameterized-query-using-prepared-statement/ --




        We then added those two columns value in the input tuple in sequential order and passed SQL update query and input tuple t0 cursor.execute() function, remember tuple contain user data in sequential order of placeholders.



        The CURSOR’s execute() method takes an optional second argument containing a list of data values. each entry in the list must be in tuple format to associate with parameter markers in the statement.







        share|improve this answer













        https://pynative.com/python-mysql-execute-parameterized-query-using-prepared-statement/ --




        We then added those two columns value in the input tuple in sequential order and passed SQL update query and input tuple t0 cursor.execute() function, remember tuple contain user data in sequential order of placeholders.



        The CURSOR’s execute() method takes an optional second argument containing a list of data values. each entry in the list must be in tuple format to associate with parameter markers in the statement.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 23:50









        Rick JamesRick James

        70.2k563103




        70.2k563103
































            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%2f53328746%2fpython-delete-mysql-mariadb-with-a-python-variable-not-working%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

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues