无法通过pyodbc将日期和时间插入到sql server中 [英] Can't insert Date and Time to sql server via pyodbc

查看:129
本文介绍了无法通过pyodbc将日期和时间插入到sql server中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Linux(raspbian)环境中使用python语言将日期和时间插入到SQL服务器中。至目前为止,我可以连接到MS Sql,并且我还使用pyodbc创建了一个表和im。

 #! / user / bin / env python 
import pyodbc
import datetime

dsn ='nicedcn'
user = myid
password = mypass
database = myDB

con_string ='DSN =%s; UID =%s; PWD =%s; DATABASE =%s;'%(dsn,用户,密码,数据库)
cnxn = pyodbc.connect(con_string)
cursor = cnxn.cursor()

string =CREATE TABLE Database3([row name] varchar(20),[my date] date),[我的时间]时间)
cursor.execute(string)
cnxn.commit()

这部分没有任何错误,这意味着我已经成功地创建了一个表吗?或者有什么问题吗?



我尝试以这种方式添加日期和时间。

 code> now = datetime.datetime.now()
d1 = now.date()
t2 = now.strftime(%H-%M-%S)
cursor.execute(insert into Database3([row name],[my date],[my time])values(?,?,?),
('new1',d1,t2))
cnxn.commit()

但是我收到这个错误。 pythonc.exe文件错误:


('HY004','[HY004] [FreeTDS] [SQL Server]无效的数据类型(O)
(SQLBindParameter)')


请帮助我。感谢提前

解决方案

如果您在Windows上,请安装最新版本的 SQL Server的Microsoft ODBC驱动程序,以确保 DATE TIME 类型。



如果您使用的是Linux,请使用页面以确定可用于您的分发的最新驱动程序。 p>

使用参数占位符并将值传递为日期时间对象当前日期时间值。

  now = datetime.datetime.now()
sql =insert into Database3([行名],[我的日期],[我的时间])值(?,?,?)
curso r.execute(sql,('new1',now.date(),now.time()))
cnxn.commit()

请注意,上述代码仅在Windows上测试。


I'm trying to insert date and time to SQL server in Linux (raspbian) environment using python language.So far i was able connect to MS Sql and also i created a table and im using pyodbc.

#! /user/bin/env python
import pyodbc 
import datetime

dsn = 'nicedcn'
user = myid
password = mypass
database = myDB

con_string = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (dsn, user, password, database)
cnxn = pyodbc.connect(con_string)
cursor = cnxn.cursor()

string = "CREATE TABLE Database3([row name] varchar(20), [my date] date), [my time]    time)"
cursor.execute(string)
cnxn.commit()

This part complied without any error.That means i have successfully created a table right? Or is there any issue?

I try to add date and time this way.

   now = datetime.datetime.now()
    d1 = now.date()
    t2 = now.strftime("%H-%M-%S")
    cursor.execute("insert into Database3([row name], [my date], [my time]) values (?,?,?)", 
    ('new1', d1, t2))
    cnxn.commit()

But i get this error. pyodbc.ProgrammingError:

('HY004', '[HY004] [FreeTDS] [SQL Server]Invalid data type (O) (SQLBindParameter)')

help me please. thanks in advance

解决方案

If you are on Windows, install the latest version of the Microsoft ODBC Driver for SQL Server to ensure the DATE and TIME types are supported.

If you are on Linux, use this page to determine the latest driver available for your distribution.

Use parameter placeholders and pass the values as date and time objects for the current datetime value.

now = datetime.datetime.now()
sql = "insert into Database3([row name], [my date], [my time]) values (?,?,?)"
cursor.execute(sql, ('new1', now.date(), now.time()))
cnxn.commit()

Note that the above code was only tested on Windows.

这篇关于无法通过pyodbc将日期和时间插入到sql server中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆