I am using Duckdb to insert data by Batch Insert
While using following code
conn.execute('INSERT INTO Main SELECT * FROM df')
I am getting following error
Invalid Input Error: Failed to cast value: Unimplemented type for cast (VARCHAR -> NULL)
I tried using
df.fillna('N/A')
to fill any null values to avoid the error but still I am getting the same error. How to fix this?
Related
my table
I got a problem when I try to use UPDATE to update my database. My table has 6 columns (datatype are str, datetime, int, int, float, and int, respectively). I write a python command as follow:
cur.execute("UPDATE DISCHARGE1 SET DataValue=200, flag=1 WHERE Station='12345' and DataDate='12/16/2021' and DataHour=1 and DataMinute=0")
then I got error:
cur.execute("UPDATE DISCHARGE1 SET DataValue=200, flag=1 WHERE Station='12345' and DataDate='12/16/2021' and DataHour=1 and DataMinute=0")
pyodbc.DataError: ('22018', '[22018] [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression. (-3030) (SQLExecDirectW)')
I checked and realized the problem is in the datatype of DataDate column. I then remove ' ' and the code run without error but nothing is updated. Despite the datatype of the DataDate column is datetime, the datatype required in "UPDATE" line is float. Somebody help me
To update your datas you need to commit with connection.commit() after your cur.execute
Documentation :
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-commit.html
Edit:
Have you tried to send the date like this
DataDate=Format('12/16/2021', "mm/dd/yyyy hh:nn:ss am/pm")
or
DataDate=DateValue('12/16/2021')
I'm using MySQL connector in Python and trying to insert an integer data, but I keep getting this error:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s)' at line 1
My code looks like this:
medDosage = int(''.join(filter(str.isdigit, '42mg')))
myCursor.execute("INSERT INTO dosage (dosageDesc) VALUES (%s)", medDosage)
db.commit()
And this statement has been working just fine for all other variables, and somehow for INT value, it does not work. I tried inserting the string variable instead of int, but doesn't work. Also tried to convert the value such as int(medDosage) to make sure it's the right type, but still doesn't work. I know my syntax is correct so I cannot really understand the error. Can you please help why this error is showing?
Thank you in advance.
You need to ensure the last argument is a tuple:
myCursor.execute("INSERT INTO dosage (dosageDesc) VALUES (%s)", (medDosage,))
I am trying to make the select statement dependent on an args variable I am passing via a python script (in this case args.type='hello').
It looks like this:
case when '{{type}}' = 'hello'
then
SELECT
name from table1
else
SELECT
city from table2 where code='usa'
end
The error I am getting:
syntax error unexpected 'case'.
syntax error unexpected 'else'.
syntax error unexpected ')'.
I also tried IFF clause but did run into same issues.
If you were sending this sql to snowflake and it was failing due to a syntax error, I would expect you to get an error like "SQL compilation error: ...". Therefore I wonder if the issue isn't in your python program.
Could you share more?
Where you trying to set some paramters?
The snowflake python connector supports several syntaxes:
format: .execute("... WHERE my_column = %s", (value,))
pyformat: .execute("... WHERE my_column = %(name)s", {"name": value})
qmark: .execute("... WHERE my_column = ?", (value,))
numeric: .execute("... WHERE my_column = :1", (value,))
If you are using python3 you can use something like f" {pythonvar} ".
Could you give us more context about what you are doing?
I am trying to insert rows into a Postgres database from a pandas dataframe. I am packaging up the dataframe as follows:
records = records.apply(lambda row: records(**row), axis=1)
Then committing as follows:
for record in records:
if not records(database.config.db_session, record.id_coda):
database.config.db_session.add(record)
database.config.db_session.flush()
database.sqlalchemy.db_session.commit()
However I keep getting the error:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'Series'
I have checked all datatypes which match the expected values for the class and I've removed any functions i was applying to the series elements. Neither has worked.
Would really appreciate some help.
I am inserting array values to mysql but it throws some error.
Could not find where does the error occured and where is the error.
Error:
TypeError: 'Connection' object does not support indexing
Where is the error and what is the fix?
Your db variable contains a Connection object instance. This Connection objects are not indexable, so db[0]....db[n] will not provide any values to be inserted in the SQL query.
You probably meant this
db1[1],db1[2]
Instead of this
db[1],db[2]