使用python将数据插入DB时参数转换为TypeError

时间:2019-03-25 13:41:22

标签: mysql python-3.x

发现了有关此主题的一些问题,但没有一个能真正解决我的问题。

在尝试向SQL服务器中插入数据时遇到了一些问题,关于数据库,我是一个新手,所以我猜想我定义查询或值的方式出了问题。

我的代码如下:

query = "INSERT INTO `Productos`(`IDProductos`, `ItemID`, `Nombre`, `MPN`, `Descripcion`, `URL`, `Precio`, `Departamento`, `CodigoGenerico`, `Fecha`, `Procedencia`) VALUES (?,?,?,?,?,?,?,?,?,?,?)"

for index,row in df.iterrows():

    IDProductos = '???'
    ItemID = row['codigoEspecificoProducto']
    Nombre = row['nombreProducto']
    MPN = 'null'
    Descripcion = 'null'
    URL = row['urlProducto']
    Precio = row ['precioProducto']
    Departamento = row['categoriaProducto']
    CodigoGenerico = row['codigoGenericoProducto']
    Fecha = 'null'
    Procedencia = 'Amazon'

    values = [IDProductos,ItemID,Nombre,MPN,Descripcion,URL,Precio,Departamento,CodigoGenerico,Fecha,Procedencia]

    cursor.execute(query,values)

我正在做的基本上是将数据从excel文件传递到数据库。

我使用的查询正确吗?这是我复制出现在数据库中的insert所得到的。

TypeError: not all arguments converted during string formatting到达cursor.execute(query,values)时,我得到了

1 个答案:

答案 0 :(得分:0)

从Excel到SQL Server。

'将引用设置为:Microsoft ActiveX数据对象6.1库

Sub InsertInto()

'Declare some variables
Dim cnn As adodb.Connection
Dim cmd As adodb.Command
Dim strSQL As String

'Create a new Connection object
Set cnn = New adodb.Connection

'Set the connection string
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Northwind;Data Source=Your_Server_Name"

'Create a new Command object
Set cmd = New adodb.Command

'Open the Connection to the database
cnn.Open

'Associate the command with the connection
cmd.ActiveConnection = cnn

'Tell the Command we are giving it a bit of SQL to run, not a stored procedure
cmd.CommandType = adCmdText

'Create the SQL
strSQL = "UPDATE TBL SET JOIN_DT = '2013-01-22' WHERE EMPID = 2"

'Pass the SQL to the Command object
cmd.CommandText = strSQL


'Execute the bit of SQL to update the database
cmd.Execute

'Close the connection again
cnn.Close

'Remove the objects
Set cmd = Nothing
Set cnn = Nothing

End Sub