设置sqlite临时存储目录 [英] Setting sqlite temp store directory

查看:20
本文介绍了设置sqlite临时存储目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python 二进制文件,它使用 SQLite 作为其后端数据库.SQLite 的文档和代码建议设置以下 3 个环境变量中的任何一个都应该起作用:

I have a Python binary which uses SQLite as its backend database. SQLite's documentation and the code suggests that setting any of the following 3 environment variables should work:

export TMP=/var/tmp/sqlite/
export TEMP=/var/tmp/sqlite/
export TEMPDIR=/var/tmp/sqlite/

如果我在启动 Python 二进制文件之前在 bash 脚本中导出上述变量,则无济于事.

If I export the above variables in my bash script just before I start my Python binary, it does not help.

我尝试的另一个选项是通过设置 os.environ 来调用 putenv():

Another option I tried is calling putenv() by setting os.environ:

os.environ['TMP'] = /var/tmp/sqlite/
os.environ['TEMP'] = /var/tmp/sqlite/
os.environ['TEMPDIR'] = /var/tmp/sqlite/

以上选项都没有帮助说服 SQLite 使用 /var/tmp/sqlite 作为其临时存储目录.唯一有效的选项 - SQLite 的文档说已弃用 - 是设置 temp_store_directory pragma 语句:

None of above options has helped in persuading SQLite to use /var/tmp/sqlite as its temp store directory. The only option that has worked - which SQLite's documentation says is deprecated - is setting the temp_store_directory pragma statement:

PRAGMA temp_store_directory = '/egnyte/.work/sqlite_temp'

既然使用 pragma 语句不是我想要的选择,那么还有什么其他技巧吗?

Since using the pragma statement is not the choice I would like to make, is there any other trick?

推荐答案

您所指的环境变量确实是 sqlite 寻找的,但在 Windows 中,而不是在 UNIX 中.

The environment variables you are referring to are indeed what sqlite looks for but in Windows, not UNIX.

在Unix中,你需要设置的环境变量是TMPDIR,如sources所示:

In Unix, the environment variable you need to set is TMPDIR as shown in the sources:

static const char *unixTempFileDir(void){
  static const char *azDirs[] = {
     0,
     0,
     "/var/tmp",
     "/usr/tmp",
     "/tmp",
     0        /* List terminator */
  };
  unsigned int i;
  struct stat buf;
  const char *zDir = 0;

  azDirs[0] = sqlite3_temp_directory;
  if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
    if( zDir==0 ) continue;
    if( osStat(zDir, &buf) ) continue;
    if( !S_ISDIR(buf.st_mode) ) continue;
    if( osAccess(zDir, 07) ) continue;
    break;
  }
  return zDir;
}

这篇关于设置sqlite临时存储目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆