Larry
Larry

Reputation: 45

Change Datatype of Values in a Pandas SQL Insert Statement

Using Pandas read_sql() function to INSERT data into a SQL table called table_1. Pulling data from a primary database and writing to table_1.

# Creates the table

    read_sql(
        f"""
        CREATE TABLE IF NOT EXISTS {table_1} (id varchar, centroid varchar, date int, thresh bigint)

        """
    )

Using a loop to process multiple days and calling function_1.

date_format = "%Y%m%d"
dates_to_compute = pd.date_range(start='2022-09-01', end='2022-09-10', freq='D').strftime(date_format)

for date in dates_to_compute:
         print(f"Executing date {date}")
         query = f"""
              INSERT INTO {table_1}
              {function_1(id, centroid, date, thresh)}
         """
         read_sql(query)

Here is the error statement:

DatabaseError:
Insert query has mismatched column types:
Table: [varchar, varchar, integer, bigint],
Query: [bigint, varchar, double, double, array(varchar(9))]

My question is can I modify the INSERT statement to change the datatypes so that they match those of table_1 that was created earlier.

Upvotes: 0

Views: 121

Answers (0)

Related Questions