0

I'm trying to insert some data to a SQL Server database using python script.

cursor.execute(''' insert into TM_VISITAS(
                    idVisita,
                    sesion,
                    idCliente,
                    idPhygi
                    )VALUES(
                    NEXT VALUE FOR seq_visitas,
                    ?,
                    ?,
                    ?
                ) ''',
                (
                total_dict.get("Session_id") ,
                total_dict.get("Client_id"),
                total_dict.get("Device_id") 
                )
            ) 

but this is what I receive:

pyodbc.ProgrammingError: ("A TVP's rows must be Sequence objects.", 'HY000')

Can someone tell me how to insert a sequence value using python?

Dale K
  • 25,246
  • 15
  • 42
  • 71
Alex Ice
  • 1
  • 1
  • 1

2 Answers2

2

I have had the same problem. I was able to better understand why here: python list in sql query as parameter

Moreover: I think you don't need the parenthesis after the comma inside the execute statement

Take a look here:

self.cursor.execute('INSERT INTO test_table VALUES (?,?,?,?,?,?,?,?)',
                    country_id,
                    product_id,
                    project_name,
                    description,
                    license_type,
                    date,
                    valid,
                    confirmed)

I have had the same error as you, it was caused because the date attribute was a list

Dharman
  • 30,962
  • 25
  • 85
  • 135
-1

it worked for me when I created the insert as stored proc.

    sql_query = "DECLARE @RC int; EXEC @RC = [dbo].[sp] 
    @id=%s,@nm='%s',@module=%s,@type='%s',@file='%s'; SELECT @RC AS rc;"%(str(id), 
    'sales.csv', 1, 'SALES','sales_shift')

PS: The sequence is not an input here but its part of the sp like

    procedure [dbo].[sp] (@id int,
        @nm varchar(100),
        @module int,
        @fact varchar(30),
        @file varchar(50))
    AS

    BEGIN
    SET NOCOUNT ON;
    DECLARE @cur_val bigint;        
    SELECT @cur_val = NEXT VALUE FOR dbo.seq
    INSERT INTO table(seq_id,id,nm,time,status,m_id, type,type_name)VALUES (@cur_val,@id,@nm,getdate(),'READY',@module, @type, @file);
Patrick Klein
  • 1,161
  • 3
  • 10
  • 23