python实现数据库中两行相减并依次更新(类似于加速度计算)

#数据库中的数据为轨迹数据,包括多辆出租车的多时段定位。

因此首先根据出租车编号,定位时间排序,这样可以做到依次更新。

由于update必须要有一个where条件,因此添加ID作为标识键。

首先计算出每辆车所有定位点的加速度,其长度与表的长度一致,然后依次更新即可。

# encoding: utf-8
import pymssql
import numpy as np
import arcpy
server='localhost'
user='userb'
password='123456'
database='master'
print 'lianjie'
outGDB=r"E:\day1110.gdb"
sr = arcpy.SpatialReference(32650)
conn = pymssql.connect(server, user, password, database)
cursor = conn.cursor()
#依次读出24小时的
count=0

sql="select * from outputcs where [列 1] ='c2d8625f7acffcb5a12d4dfb84a00116'"
#sql='SELECT DISTINCT * FROM day1110 where [列 3] >20121110'+curH+curMin+"00 and [列 3]<20121110"+curH+str(nextMin)+"00"
cursor.execute(sql)
last=0
cur=1
count=0
resultv=[]
#计算加速度
for rowOne in cursor:
    if(count==0):
        last=rowOne[4]
        cur=rowOne[4]
    else:
        #
        last=cur
        cur=rowOne[4]
    v= float(cur.encode("utf-8"))- float(last.encode("utf-8"))
    resultv.append(v)
    print last,cur
    count += 1
count=0
#更新列值
for value in resultv:
    count += 1
    sql = "update outputcs set a="+str(value)+" where [ID] ="+str(count)
    print sql
    cursor.execute(sql)
    conn.commit()

print (count)

conn.close()

猜你喜欢

转载自blog.csdn.net/A873054267/article/details/87639479