Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-在Ms Sql中插入日期时间字段_Python_Sql_Sql Server_Linux - Fatal编程技术网

python-在Ms Sql中插入日期时间字段

python-在Ms Sql中插入日期时间字段,python,sql,sql-server,linux,Python,Sql,Sql Server,Linux,我想在Ms SQL DB中插入一个日期。我该怎么做 以下是我正在做的:- a = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S") data = {'AWB_Number':'1','Weight':'1','Length':'1','Height':'1','Width':'1','Customer_Name':'Naaptol','Scan_Time': a,'Series_Flag':'Others'} data = (

我想在Ms SQL DB中插入一个日期。我该怎么做

以下是我正在做的:-

a = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
data =  {'AWB_Number':'1','Weight':'1','Length':'1','Height':'1','Width':'1','Customer_Name':'Naaptol','Scan_Time': a,'Series_Flag':'Others'}

data = (
        data['AWB_Number'], data['Weight'], data['Length'], data['Height'],
        data['Width'], data['Customer_Name'], data['Scan_Time'] ,data['Series_Flag']
        )

print data


con_string = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (aramex_dsn, aramex_user, aramex_password, aramex_database)
cnxn = pyodbc.connect(con_string)

cursor = cnxn.cursor()

cursor.execute("insert into data_AutoScale_DELHUB VALUES (%s, %s, %s, %s, %s, %s, %s, %s)" % data)
cnxn.commit()

cnxn.close()
它返回一个错误,表示

Traceback (most recent call last):
  File "tests.py", line 39, in <module>
    cursor.execute("insert into data_AutoScale_DELHUB VALUES (%s, %s, %s, %s, %s, %s, %s, %s)" % data)
pyodbc.ProgrammingError: ('42000', "[42000] [FreeTDS][SQL Server]Incorrect syntax near '09'. (102) (SQLExecDirectW)")
正如我所看到的,我认为对于数据库中的datetime值,必须有一个datetime.datetime对象,而不是字符串。 所以,只要更换

a = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")

正如我所看到的,我认为对于数据库中的datetime值,必须有一个datetime.datetime对象,而不是字符串。 所以,只要更换

a = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")


对于任何在MS Access数据库中登陆并希望执行此操作的人,您可以将数据库表中的字段定义为日期/时间类型,然后将日期时间值作为单引号分隔的字符串放入INSERT或UPDATE查询中

此字符串的格式必须能被Access识别为日期/时间,例如:“2/19/2018 11:44:22 PM”或“02/19/2018 23:44:22”。ODBC驱动程序负责其余的工作,日期时间将作为有效的数据库日期/时间在表中结束

我还没有用MS-SQL尝试过这一点,但经验表明,它的工作方式应该大致相同。以下是一些为MS Access创建正确格式字符串的代码:

import pandas as pd
def MSDate_Format_from_Article(self, datestr: str) -> str:
    # Start with the format: 2018-02-14T21:57:55Z
    try:
        datetime_obj = pd.to_datetime(datestr)
    except:
        log("ERROR: Bad Date!")
        return "01/01/1980 00:00:00"
    else:
        year = "{:04d}".format(datetime_obj.year)
        month = "{:02d}".format(datetime_obj.month)
        day = "{:02d}".format(datetime_obj.day)
        hour ="{:02d}".format(datetime_obj.hour)
        minute = "{:02d}".format(datetime_obj.minute)
        second = "{:02d}".format(datetime_obj.second)
        # Return the date as a string in the format: 02/19/2018 23:44:22
        return month + '/' + day + '/' + year + ' ' + hour + ':' + minute + ':' + second

对于任何在MS Access数据库中登陆并希望执行此操作的人,您可以将数据库表中的字段定义为日期/时间类型,然后将日期时间值作为单引号分隔的字符串放入INSERT或UPDATE查询中

此字符串的格式必须能被Access识别为日期/时间,例如:“2/19/2018 11:44:22 PM”或“02/19/2018 23:44:22”。ODBC驱动程序负责其余的工作,日期时间将作为有效的数据库日期/时间在表中结束

我还没有用MS-SQL尝试过这一点,但经验表明,它的工作方式应该大致相同。以下是一些为MS Access创建正确格式字符串的代码:

import pandas as pd
def MSDate_Format_from_Article(self, datestr: str) -> str:
    # Start with the format: 2018-02-14T21:57:55Z
    try:
        datetime_obj = pd.to_datetime(datestr)
    except:
        log("ERROR: Bad Date!")
        return "01/01/1980 00:00:00"
    else:
        year = "{:04d}".format(datetime_obj.year)
        month = "{:02d}".format(datetime_obj.month)
        day = "{:02d}".format(datetime_obj.day)
        hour ="{:02d}".format(datetime_obj.hour)
        minute = "{:02d}".format(datetime_obj.minute)
        second = "{:02d}".format(datetime_obj.second)
        # Return the date as a string in the format: 02/19/2018 23:44:22
        return month + '/' + day + '/' + year + ' ' + hour + ':' + minute + ':' + second

你能告诉我们数据的真实价值吗?我已经提供了。请仔细查看
数据
我没有'a'的值,所以不是'data'的所有值− 我认为错误出在这个值上,因为MS-SQL需要一个日期时间,所以这个值可能没有正确设置。我如何设置它呢?你能给我们提供数据的真实值吗?我已经提供了。请仔细查看
数据
我没有'a'的值,所以不是'data'的所有值− 我认为此值上存在错误,因为MS-SQL需要一个日期时间,因此可能该值设置不正确。如何正确设置它?然后是否需要对
值(%s,%s,%s,%s,%s,%s,%s)
进行任何更改
%s
对于datetime是否需要更改为其他内容?您可以这样做:cursor.execute(“插入数据”\u AutoScale\u DELHUB值(?,,,,,,,,,,,,,,,,,,?)”,list(data)),我想如果您想正确使用pyodbc,至少应该阅读GettingStarted页面:我做了
cursor.execute(“插入数据自动缩放DELHUB值(?,,,,,,,,,,,,,?,,,?)”,数据
,但它抛出了
TypeError:并非所有参数在字符串格式化过程中都转换了
。在谷歌搜索时,它用
[数据]
替换
数据
,现在它说
pyodbc.Error:('HY000',驱动程序没有提供错误!')
。这可能会导致什么结果?我将自动提交设置为True并继续读取。但仍然是相同的错误。然后是否需要对
值(%s,%s,%s,%s,%s,%s,%s,%s)
%s
的日期时间需要更改为其他值?您可以这样做:cursor.execute(“插入数据自动缩放DELHUB值(?,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,如果你想正确使用pyodbc,你应该阅读它的文档,至少是GettingStarted页面:我做了
cursor.execute(“插入数据自动缩放DELHUB值(?,,,,,,,,,,,,,,,,?),数据
,但它抛出了
TypeError:在字符串格式化过程中并非所有参数都转换了
。在谷歌搜索时,它将
数据
替换为
[data]
,现在显示为
pyodbc.Error:('HY000',驱动程序没有提供错误!')
。这可能会导致什么结果?我将自动提交设置为True并继续读取。但仍然是相同的错误。