使用Python将具有日期时间和多行的数据插入到SQL表中

时间:2018-09-04 18:46:13

标签: sql sql-server tsql python-3.5

我用以下代码创建了sql表:

CREATE Table [dataISO].[dbo].[LMP](
    [DateTime] [datetime] NOT NULL, 
    [ISO] [nvarchar](50) NOT NULL,
    [pwrLoc] [nvarchar](50) NOT NULL,
    [da] [int] NULL,
    [rt] [int] NULL
        CONSTRAINT [PK_daLMP] PRIMARY KEY CLUSTERED
        (
            [DateTime] ASC,
            [ISO] ASC, 
            [pwrLoc] ASC
        )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = ON, ALLOW_ROW_LOCKS = ON, 
        ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 20) ON [PRIMARY]
        ) ON [PRIMARY]

我有一个数据列,其列为:

data.ix[1:4]
             DateTime    ISO  pwrLoc            DA             RT
1 2013-09-05 02:00:00  ERCOT  LZ_AEN            24.66          25.4100
2 2013-09-05 03:00:00  ERCOT  LZ_AEN            23.17          24.3750
3 2013-09-05 04:00:00  ERCOT  LZ_AEN            22.20          23.9225
4 2013-09-05 05:00:00  ERCOT  LZ_AEN            22.45          23.9625

我正在尝试以下操作将data插入表LMP

    values = [tuple(row) for row in data.values]
    dateInfoQry = 'insert into [dataISO].[dbo].[LMP] (DateTime, ISO, pwrLoc, da, rt) values {}'.format(values)
    conn = pyodbc.connect(r'Driver={SQL SERVER}; SERVER=2UA7491NP7\SQLEXPRESS')
    conn.execute(dateInfoQry)

我收到以下错误:

pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]The identifier that starts with '(Timestamp('2013-09-05 01:00:00'), 'ERCOT', 'LZ_AEN', 28.31, 34.5325), (Timestamp('2013-09-05 02:00:00'), 'ERCOT', 'LZ_AEN', 24.' is too long. Maximum length is 128. (103) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '(Timestamp('2013-09-05 01:00:00'), 'ERCOT', 'LZ_AEN', 28.31, 34.5325), (Timestamp('2013-09-05 02:00:00'), 'ERCOT', 'LZ_AEN', 24.'. (102)")

3 个答案:

答案 0 :(得分:4)

当我打印查询dateInfoQry时,得到以下结果:

insert into [dataISO].[dbo].[LMP] (DateTime, ISO, pwrLoc, da, rt) values [('2013-09-05 02:00:00', 'ERCOT', 'LZ_AEN', 24.66, 25.41), ('2013-09-05 03:00:00', 'ERCOT', 'LZ_AEN', 23.17, 24.375), ('2013-09-05 04:00:00', 'ERCOT', 'LZ_AEN', 22.2, 23.9225), ('2013-09-05 05:00:00', 'ERCOT', 'LZ_AEN', 22.45, 23.9625)]

您可以看到仍然有一些方括号,因为您将列表values作为一个整体包含在内(请参见.format(values))。但是,如果我使用.format(','.join(str(v) for v in values))从该字符串构建一个字符串,则会得到以下结果,该结果应该起作用:

insert into [dataISO].[dbo].[LMP] (DateTime, ISO, pwrLoc, da, rt) values ('2013-09-05 02:00:00', 'ERCOT', 'LZ_AEN', 24.66, 25.41),('2013-09-05 03:00:00', 'ERCOT', 'LZ_AEN', 23.17, 24.375),('2013-09-05 04:00:00', 'ERCOT', 'LZ_AEN', 22.2, 23.9225),('2013-09-05 05:00:00', 'ERCOT', 'LZ_AEN', 22.45, 23.9625)

答案 1 :(得分:4)

错误表明字符串太长,可能在这里插入语句的格式不正确。 始终尝试在SQL Server中对字符串使用单引号。

该字符串具有“ []”,因此也需要设置格式。

 insert into [dataISO].[dbo].[LMP] (DateTime, ISO, pwrLoc, da, rt) values [('2013-09-05 02:00:00', 'ERCOT LZ_AEN', '24.66', '25.4100'),('2013-09-05 02:00:00', 'ERCOT LZ_AEN', '24.66', '25.4100')]

因此,如果我们将代码修改为类似的内容

values = [tuple(row) for row in data]
 dateInfoQry = 'insert into [dataISO].[dbo].[LMP] (DateTime, ISO, pwrLoc, da, rt) values {}'.format(','.join(str(s) for s in values))

insert语句看起来还可以。

insert into [dataISO].[dbo].[LMP] (DateTime, ISO, pwrLoc, da, rt) values ('2013-09-05 02:00:00', 'ERCOT', 'LZ_AEN', 4.66, 5.41),('2013-09-05 02:00:00', 'ERCOT', ' LZ_AEN', 24.66, 25.41)

运行正常。

试一下

答案 2 :(得分:1)

尝试添加:

设置QUOTED_IDENTIFIER OFF

在您插入之前应该会有所帮助,因为[]与SQL中的“”类似。