0

I am inserted data successfully. but i want to get the result whether data is inserted or not. my code is:

unit_type.to_sql(con=self.mysql_hermes.conn, name='CiqHistEleData',
                                                     if_exists='append', flavor='mysql', index=False)

i want result in one variable true or false.

1
  • Would you be so kind as to mark the answer as correct as discussed so that it would be usefull to others. thanks Commented Jan 9, 2017 at 12:47

1 Answer 1

1

Sorry, that's not going to work because the to_sql method does not return a value. The usual approach in pandas is to raise exceptions rather than return True/False so in the same spirit you will have to sorround your code with try/except. TypeError and ValueError are two typical exceptions raised by to_sql

try:
    unit_type.to_sql(con=self.mysql_hermes.conn, name='CiqHistEleData',
                                                     if_exists='append', flavor='mysql', index=False)
    # save successfull
except TypeError:
    # save failed write some code

    pass
except ValueError:
    # save failed write some code

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

3 Comments

Yup I think this is the solution using try catch but i try to find some other solution, np i will do with this solution. Thanks.. @e4c5
for another solution to exist it will have to return a value which it does not. You will have to catch these exceptions anyway because if you don't your program will fail so this is the one and only option :-)
Ok, and Thanks..@e4c5

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.