通过python在mssql中插入变量

时间:2017-09-20 07:45:10

标签: python excel

我正在尝试创建一个从Excel导入数据到mssql的脚本。

我从Excel读取数据并分配给变量。它有效,我正在通过打印进行检查。连接数据库和插入字符串也可以正常工作。但它不插入变量。请帮我找一个解决方案。

python脚本:

for rownum in range(1,sheet.nrows): 
    if sheet.row_values(rownum)[0]=='':
        pass
    else:
        vals=[sheet.row_values(rownum)]
        val = sheet.cell_value(rownum, 1)
        val2=sheet.cell_value(rownum, 2)
        val3=sheet.cell_value(rownum, 3)
        val1=sheet.cell_value(rownum, 0)
        val4=sheet.cell_value(rownum, 4)
        val5=sheet.cell_value(rownum, 5)
        val1= val1.encode("utf-8")
        val4 = val4.encode("utf-8")
        val5 = val5.encode("utf-8")
        dat=int(val)
        dt=date.fromordinal(date(1990,1,1).toordinal()+dat-2)
        x = int(val2 * 24 * 3600)
        x3 = int(val3 * 24 * 3600)# convert to number of seconds
        m = time(x//3600, (x%3600)//60, x%60)
        m3 = time(x3//3600, (x3%3600)//60, x3%60)
        cursor.executemany ("INSERT INTO [sample.dbo.tv_prot_virt_im](chan_name,pr_date,start_time,end_time,prog_name,pr_genre) VALUES(?,?,?,?,?,?)", [val1,dt,m,m3,val4,val5])
        connection.commit()
 Traceback (most recent call last):
  File "C:/Users/akylyshbekova/Desktop/PyQT/main_test.py", line 26, in <module>
    tab = Table('dbo.tv_prot_virt_im',metadata,autoload=True)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\sql\schema.py", line 439, in __new__
    metadata._remove_table(name, schema)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\sql\schema.py", line 434, in __new__
    table._init(name, metadata, *args, **kw)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\sql\schema.py", line 514, in _init
    include_columns, _extend_on=_extend_on)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\sql\schema.py", line 540, in _autoload
    _extend_on=_extend_on
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 2044, in run_callable
    with self.contextual_connect() as conn:
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 2112, in contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 2151, in _wrap_pool_connect
    e, dialect, self)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 1465, in _handle_dbapi_exception_noconnection
    exc_info
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
    return fn()
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 387, in connect
    return _ConnectionFairy._checkout(self)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
    rec = pool._do_get()
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
    self._dec_overflow()
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
    return self._create_connection()
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
    return _ConnectionRecord(self)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
    self.__connect(first_connect_check=True)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 651, in __connect
    connection = pool._invoke_creator(self)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\strategies.py", line 105, in connect
    return dialect.connect(*cargs, **cparams)
  File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\default.py", line 393, in connect
    return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][\xc4\xe8\xf1\xef\xe5\xf2\xf7\xe5\xf0 \xe4\xf0\xe0\xe9\xe2\xe5\xf0\xee\xe2 ODBC] \xc8\xf1\xf2\xee\xf7\xed\xe8\xea \xe4\xe0\xed\xed\xfb\xf5 \xed\xe5 \xed\xe0\xe9\xe4\xe5\xed \xe8 \xed\xe5 \xf3\xea\xe0\xe7\xe0\xed \xe4\xf0\xe0\xe9\xe2\xe5\xf0, \xe8\xf1\xef\xee\xeb\xfc\xe7\xf3\xe5\xec\xfb\xe9 \xef\xee \xf3\xec\xee\xeb\xf7\xe0\xed\xe8\xfe (0) (SQLDriverConnect)')

Process finished with exit code 1

0 个答案:

没有答案