1

I have been banging my head against this wall for a while now. I can't get this insert query to work. Is it because I have multiple data types(int, bool, strings, NoneType) that I'm inserting and I'm using %s ? Any help would be greatly appreciated. Thanks!

class DBWork(object):
    def __init__(self, attackerip=None, victimip=None, shunlen=15, shuncount=1, shunreason=None, shunepoch=None,
                 shunerrorbool=False, shunerror=None, paloaddedepoch=None, paloremovedepoch=None,
                 lastshunlen=15):
        self.Id = None
        self.attackerip = attackerip
        self.victimip = victimip
        self.shunlen = shunlen
        self.shuncount = shuncount
        self.shunreason = shunreason
        self.shunepoch = shunepoch
        self.shundatetime = 'need to build this'
        self.shunerrorbool = shunerrorbool
        self.shunerror = shunerror
        self.paloaddedepoch = paloaddedepoch
        self.paloremovedepoch = paloremovedepoch
        self.lastshunlen = lastshunlen
        self.shunlen = shunlen
        self.newshunlen = lastshunlen * shuncount

    def HistoryInsert(self):
        insert_query = (
            "INSERT INTO `autoshun`.`shunhistory` "
            "(Id, attackerip, victimip, shunlen, shuncount, shunreason, shunepoch, shundatetime, shunerrorbool, "
            "shunerror, paloaddedepoch, paloremovedepoch) "
            "VALUES "
            "(NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s );"
        )
        values = [None, self.attackerip, self.victimip, self.shunlen, self.shuncount, self.shunreason, self.shunepoch,
                  self.shundatetime, self.shunerrorbool, self.shunerror, self.paloaddedepoch, self.paloremovedepoch]
        print values
        self.cur.execute(insert_query, values)

sample = DBWork(attackerip='1.1.1.15', victimip='2.2.2.2',  shunlen=15, shuncount=1, shunreason='hax', shunerrorbool=True)

sample.DBConnect()
sample.HistoryInsert()

values being passed in:

values = [None, '1.1.1.15', '2.2.2.2', 15, 1, 'hax', 'NULL', 'need to build this', False, 'NULL', 'NULL', 'NULL']

Error:

Traceback (most recent call last):
  File "/home/dobbs/PycharmProjects/autoshun/sqlobjects.py", line 102, in <module>
    sample.HistoryInsert()
  File "/home/dobbs/PycharmProjects/autoshun/sqlobjects.py", line 95, in HistoryInsert
    self.cur.execute(insert_query, values)
  File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 164, in execute
    query = self.mogrify(query, args)
  File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 143, in mogrify
    query = query % self._escape_args(args, conn)
TypeError: not all arguments converted during string formatting
1
  • This is a typo. There are 11 %s placeholders in the query (NULL does not count), but 12 values to substitute in (None counts). Commented Apr 4, 2023 at 3:05

1 Answer 1

1

Go ahead and leave out the id field. MySQL will know what to do:

    insert_query = (
        "INSERT INTO `autoshun`.`shunhistory` "
        "(attackerip, victimip, shunlen, shuncount, shunreason, shunepoch, shundatetime, shunerrorbool, "
        "shunerror, paloaddedepoch, paloremovedepoch) "
        "VALUES "
        "(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s );"
    )

    values = [self.attackerip, self.victimip, self.shunlen, self.shuncount, self.shunreason, self.shunepoch,
              self.shundatetime, self.shunerrorbool, self.shunerror, self.paloaddedepoch, self.paloremovedepoch]

As is your query has a NULL and you're trying to pass in None without a parameter for that None.

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

2 Comments

THANKS!! that was definitely it.
Cheers, mate. Happy coding to you!

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.