无法更新日期值

2024-10-03 02:34:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着研究了几个与这个问题类似的问题,但出于某种原因,它们似乎仍然不适合我

我收到错误信息

(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SET stockPriceRef.ticker='NGVT' SET stockPriceRef.updateDate='2020-05-25 01:22:1' at line 1")

我在没有时间的情况下尝试了这个方法,它很有效,但是我仍然想更新时间戳

我尝试使用的查询

'UPDATE stockPriceRef SET stockPriceRef.id=%s SET stockPriceRef.ticker=%s SET stockPriceRef.updateDate=%s SET stockPriceRef.open=%s SET stockPriceRef.high=%s SET stockPriceRef.low=%s SET stockPriceRef.close=%s SET stockPriceRef.volume=%s WHERE id=%s;'
# values tuple I am passing below:
#values: (1961, 'NGVT', '2020-05-25 01:22:18', 52.02, 52.315, 50.65, 51.45, 209950, 1961)

我这样称呼它:

 # the update statement mentioned above
 update_stock_price_ref = create_update_prepared_statement('stockPriceRef', self.sql)
 # get out current time and format it
 time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
 # call execute statement and pass values
 self.sql.execute_query(update_stock_price_ref,values=(ticker_id,ticker,time,price_summ['open'],price_summ['high'],
                                              price_summ['low'],price_summ['previousClose'], price_summ['volume'], ticker_id))

def execute_query(self, query, values=None):
        """ execute a query  """
        # if you want to use ssh password use - ssh_password='your ssh password', bellow
        data = None
        #print('executing query: {}'.format(query))
        cursor = self.sql_conn.cursor()
        try:
            if(values is None):
                data = cursor.execute(query)
                data = cursor.fetchall()
            else:
                if(type(values) is not tuple):
                    raise TypeError(f'Values should be of type tuple, type of {type(values)}')
                data = cursor.execute(query, values)
                data = cursor.fetchall()
            if('INSERT' in query or 'UPDATE' in query):
                self.sql_conn.commit()
        except Exception as ex:
            print(ex)
            print('query: {}'.format(query))
            print(f'values: {values}')
            return None
        #
        return data

我也尝试过从mySQL控制台执行该语句,但由于某些原因,它仍然可以工作,但当我从python调用它时,它就不能工作了。在这一点上,任何事情都会很有帮助

编辑: 以下是我已经研究过的一些其他问题。 updating-date-time-into-mysql-via-pythondatetime-date-value-not-updating-into-mysql-database-in-python-correctly-updatehow-can-i-update-a-timestamp-field-in-mysql-through-python

编辑2 多亏了klaus-d,他们指出我在查询中有不止一个集合。查看了mySQL文档,发现我的查询还存在一些其他问题

UPDATE stockPriceRef SET id=%s,ticker=%s,updateDate=%s,open=%s,high=%s,low=%s,close=%s,volume=%s WHERE id=%s;

Tags: inselfidexecutesqldataupdatequery
1条回答
网友
1楼 · 发布于 2024-10-03 02:34:54

使用此查询

UPDATE
   stockPriceRef 
SET
   stockPriceRef.id =% s, stockPriceRef.ticker =% s, stockPriceRef.updateDate =% s, stockPriceRef.open =% s, stockPriceRef.high =% s, stockPriceRef.low =% s, stockPriceRef.close =% s, stockPriceRef.volume =% s 
WHERE
   id =% s;

有关准备好的报表,请参阅: https://github.com/aviboy2006/flask-rest-api/blob/master/database.py#L39

相关问题 更多 >