Skip to content
Advertisement

Executing SQL query using python doesn’t do anything

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement