Java 如何在sqlite中解析和更改值
我有sqlite dbaround 10k条目,其时间以以下格式存储:hh:mmam/pm,例如12:40pm、6:50am,我需要以毫秒为单位,以便对它们进行比较。有没有办法让它发生?我正在使用JavaJava 如何在sqlite中解析和更改值,java,sqlite,Java,Sqlite,我有sqlite dbaround 10k条目,其时间以以下格式存储:hh:mmam/pm,例如12:40pm、6:50am,我需要以毫秒为单位,以便对它们进行比较。有没有办法让它发生?我正在使用Java 编辑:对不起,我的问题模棱两可。我想获取值,将其转换为毫秒,然后重写它,这样所有值都将以毫秒而不是当前格式存储 下面的python代码解决了这个问题,请发布它,以防其他人需要执行类似的操作。编程完成后,必须手动将列的类型从文本更改为数字 import sqlite3 from datetime
编辑:对不起,我的问题模棱两可。我想获取值,将其转换为毫秒,然后重写它,这样所有值都将以毫秒而不是当前格式存储 下面的python代码解决了这个问题,请发布它,以防其他人需要执行类似的操作。编程完成后,必须手动将列的类型从文本更改为数字
import sqlite3
from datetime import datetime
def unix_time(dt):
"""Takes datetime object and returns its unix time since epoch"""
epoch = datetime.utcfromtimestamp(0) #January 1st 1970
delta = dt - epoch
return delta.total_seconds()
def unix_time_millis(dt):
return unix_time(dt) * 1000 #milliseconds
db = sqlite3.connect("your_db.sqlite")#connect to initial database
cursor = db.cursor()
cursor.execute("select * from fancy_table")
all_entries = cursor.fetchall() #get our stuff
#new database. Make a copy of initial to prevent serious damage
db_new = sqlite3.connect("your_db_new.sqlite")
for entry in all_entries:
entry = str(entry[0].strip())#cursor returns tuple
#since it is time not a date, get milliseconds of the epoch
date_object = datetime.strptime("Jan 1 1970 " + entry, '%b %d %Y %I:%M%p')
new_time = unix_time_millis(date_object)
#print(entry + " to " + str(new_time))
cursor_update = db_new.cursor()#new cursor
try:
#updating
cursor_update.execute("UPDATE fancy_table SET time = '" + str(new_time) + "' WHERE arr_time = '" + entry + "'")
except Exception as error:
print(error)
db_new.commit()#needs to be commited to take affect
print("done")
您的SQLlite使用什么语言?完全忘记了,我使用的是JavaLite列的类型是什么?