我有以下代码,使用pscyopg2:
sql = 'select %s from %s where utctime > %s and utctime < %s order by utctime asc;'
data = (dataItems, voyage, dateRangeLower, dateRangeUpper)
rows = cur.mogrify(sql, data)
Run Code Online (Sandbox Code Playgroud)
这输出:
select 'waterTemp, airTemp, utctime' from 'ss2012_t02' where utctime > '2012-05-03T17:01:35+00:00'::timestamptz and utctime < '2012-05-01T17:01:35+00:00'::timestamptz order by utctime asc;
Run Code Online (Sandbox Code Playgroud)
当我执行它时,它会失败 - 这是可以理解的,因为表名周围的引号是非法的.
有没有办法合法地将表名作为参数传递,或者我是否需要执行(显式警告)字符串连接,即:
voyage = 'ss2012_t02'
sql = 'select %s from ' + voyage + ' where utctime > %s and utctime < %s order by utctime asc;'
Run Code Online (Sandbox Code Playgroud)
为任何见解干杯.
我有一个Python日期时间戳和一个大的dict(索引),其中键是时间戳,值是我感兴趣的其他一些信息.
我需要尽可能有效地在索引中找到最接近时间戳的日期时间(键).
目前我做的事情如下:
for timestamp in timestamps:
closestTimestamp = min(index,key=lambda datetime : abs(timestamp - datetime))
Run Code Online (Sandbox Code Playgroud)
哪个有效,但需要太长时间 - 我的索引字典有数百万个值,我正在进行数千次搜索.我对数据结构很灵活等等 - 时间戳大致是顺序的,所以我从第一个时间戳到最后一个时间戳进行迭代.同样,我加载到dict中的文本文件中的时间戳是顺序的.
任何优化的想法将不胜感激.
我正在开发一个应用程序,其中一部分使用OpenLayers(调用Geoserver服务的WMS)显示一些经常更新的数据(船只轨道 - 或更具体地说,一系列点).
我希望这个船只轨道在设定的时间间隔内更新 - OpenLayers.Strategy.Refresh似乎是最适合的方式.我稍微修改了wms.html示例(OpenLayers 2.11)来尝试这个,即:
underway = new OpenLayers.Layer.WMS("Underway Data",
"http://ubuntu-geospatial-server:8080/geoserver/underway/wms",
{'layers': 'underway:ss2011_v03', transparent: true, format: 'image/gif'},
{isBaseLayer: false},
{strategies : [new OpenLayers.Strategy.Refresh({interval: 6000})]}
);
map.addLayers([layer, underway]);
Run Code Online (Sandbox Code Playgroud)
据我所知,这应该按原样工作(即每隔6秒刷新一次进行中的层),但没有任何反应.底层WMS正在更新 - 如果我手动刷新地图,将显示更新的数据.
我确信我错过了一些相当明显的东西,任何帮助都会非常感激.我在Firebug中没有任何错误或其他任何错误,它只是没有做任何事情.
我的任务是对C++应用程序进行简单的更改.不幸的是,我来自Java背景,而且我遇到了一些指针问题.
有问题的代码读入给定目录中的文件列表(使用环境变量设置),并对每个文件执行某些操作.
char * rebuildDir = getenv("REBUILD_DIR");
char * currentFile;
DIR *asciiDir;
struct dirent *ent;
asciiDir = opendir(rebuildDir);
if (asciiDir != NULL)
{
while ((ent = readdir(asciiDir)) != NULL)
{
std::cout << "rebuild sensor, rebuild dir is " << getenv("REBUILD_DIR") << std::endl;
currentFile = rebuildDir;
strcat(currentFile, ent->d_name);
ifstream raw(currentFile);
while(raw)
{
...snip...
}
raw.close();
}
closedir(asciiDir);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,目的是将环境变量存储一次,然后将其复制到currentFile,然后将当前文件名连接到currentFile,准备传入ifstream.
问题是
currentFile = rebuildDir;
Run Code Online (Sandbox Code Playgroud)
不会重置为环境变量,因此strcat继续使用旧文件名并附加到它,因此:
/home/file1
/home/file2
/home/file3
Run Code Online (Sandbox Code Playgroud)
将执行为
/home/file1
/home/file1/home/file2
/home/file1/home/file2/home/file3
Run Code Online (Sandbox Code Playgroud)
通过循环.我猜我用指针犯了一个错误,但我无法追踪它.
感谢您的帮助,并为这个微不足道的问题道歉.
PS - 如果有一个明显更好的方法来完成我的任务,请随意指出:)
python ×2
algorithm ×1
c++ ×1
javascript ×1
map ×1
openlayers ×1
pointers ×1
postgresql ×1
psycopg2 ×1
search ×1
sql ×1
timestamp ×1