0

I'm coding a tracking stock prices program. I'm trying to execute INSERT query and it executes, but doesn't do anything.

Here's my method in my class SQLDatabaseOperator

def insert_data_to_database(self, ticker_name, api_data_dict):
    self.connect_to_database()

    sqlite_insert_data_query = '''INSERT INTO {}(open_price, high_price, low_price, previous_close_price, date)\
                                                  VALUES ({}, {}, {}, {}, 'data');'''.format(ticker_name,\
                                                                                            api_data_dict['o'],\
                                                                                            api_data_dict['h'],\
                                                                                            api_data_dict['l'],\
                                                                                            api_data_dict['pc'])
    cursor = self.sqliteConnection.cursor()
    cursor.execute(sqlite_insert_data_query)
    cursor.close()
    self.close_connection()

And here's my sample test.

name = 'NIO'
data = {'o': 24.123, 'h': 30.02, 'l': 22.03, 'pc': 23}
test = SQLDatabaseOperator()
test.insert_data_to_database(name, data)

Nothing is in the database. I don't know what to do. I'm using SQLite. This is my database.

enter image description here

5
  • 1
    Make sure you call self.sqliteConnection.commit() after the INSERT query. Commented Mar 29, 2021 at 21:59
  • @mechanical_meat before or after cursor.execute() ? Commented Mar 29, 2021 at 22:01
  • 1
    After cursor.execute(), but before you're closing the connection. Commented Mar 29, 2021 at 22:02
  • @mechanical_meat THANK YOU VERY MUCH, It solved my problem! Commented Mar 29, 2021 at 22:03
  • 1
    Cheers, mate. Happy coding to you! Commented Mar 29, 2021 at 22:07

1 Answer 1

1

The answear was simple - I just need to use

self.sqliteConnection.commit()

after executing cursor. Regards for https://stackoverflow.com/users/42346/mechanical-meat for solving my problem.

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

Comments

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.