如何更改SQLITE数据库以在其他任何文件夹中创建临时文件,而不是使用Java创建临时文件夹?

发布于 2025-02-09 17:12:46 字数 3227 浏览 2 评论 0原文

我在Java应用程序中使用SQLITE数据库,在该应用程序中我在运行时创建数据库,然后创建表并执行不同的DB操作,因为SQLite必须在某处创建其临时文件,而临时目录是旨在拥有此类文件的目录,当前在我的Windows OS环境中,SQLite正在使用temp文件夹创建文件,我需要在C驱动器以外的任何其他位置更改文件夹。 我已经尝试以下操作

  1. 更改temptmp的环境变量值,如下述问题如何更改sqlite创建etilqs文件的temp文件夹?
  2. 添加新的环境变量sqlite_tmpdir 和tmpdir下面提到的问题 设置sqlite temp store directory
  3. 我尝试了pragma语句遵循SQLite文档 https://www.sqlite.org/tempfiles.html

d:\ sqlite-tmp';

I have also tried with following piece of code but it didn't worked for me
Connection obj_connection = null;
        Statement obj_statement = null;
        try {
            m_objLogManager.logInfo("Creating table 'QUEUE' if not exist ...");
            obj_connection = getConnection();
            m_objLogManager.logInfo("Execute PRAGMA");
            try(Statement statement = obj_connection.createStatement()) {
                m_objLogManager.logInfo("going to Execute PRAGMA");
                statement.execute("PRAGMA temp_store_directory = 'D:\\SQLite-TMP';");
                m_objLogManager.logInfo("Execute PRAGMA successfully");
            }
            try(Statement statement = obj_connection.createStatement()) {
                m_objLogManager.logInfo("getting PRAGMA");
                try(ResultSet rs = statement.executeQuery("PRAGMA temp_store_directory;")) {
                    m_objLogManager.logInfo("getting PRAGMA successfull");
                    m_objLogManager.logInfo(rs.getString(1));
                }
            }
            obj_statement = obj_connection.createStatement();
            String str_sql = "CREATE TABLE IF NOT EXISTS QUEUE " +
                    "(Id VARCHAR(200) PRIMARY KEY     NOT NULL," +
                    " Origin            TEXT, " +
                    " Port              INT, " +
                    " RegisterAt        TEXT, " +
                    " UserName        TEXT)";
            obj_statement.executeUpdate(str_sql);
            m_objLogManager.logInfo("Table 'QUEUE' is created if not exist");
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (obj_statement != null) {
                obj_statement.close();
            }
            if (obj_connection != null) {
                obj_connection.close();
            }
        }

我也在sqlite浏览器中打开我的db文件,并在查询之后执行执行 pragma temp_store_directory ='d:\ sqlite-tmp'; 但是,仍然在temp文件夹中创建临时文件的sqlite我想将临时文件的位置更改为d:\ sqlite-tmp此文件夹。 任何人都可以帮助我,因为我陷入了这个问题,请让我知道如何设置全局变量sqlite3_temp_directory,因为我不明白如何从官方文档谢谢。

我的环境

  • Windows 10
  • Sqlite数据库
  • Java 11

I am using SQLite database within my JAVA application in which I create database on runtime and then create tables and perform different DB operations, as SQLite has to create its temporary files somewhere, and the temporary directory is the directory that is designed to hold such files, currently in my windows OS environment SQLite is creating files in TEMP folder, I need to change the folder in any other location other than C drive.
I have tried following operations

  1. Change Environment variable values of TEMP and TMP in mentioned following question How can I change the temp folder where sqlite creates etilqs files?
  2. Add new Environment variable SQLITE_TMPDIR and TMPDIR mentioned in following question
    Setting sqlite temp store directory
  3. I tried PRAGMA statements according to following SQLite documentation
    https://www.sqlite.org/tempfiles.html

PRAGMA temp_store_directory = 'D:\SQLite-TMP';

I have also tried with following piece of code but it didn't worked for me
Connection obj_connection = null;
        Statement obj_statement = null;
        try {
            m_objLogManager.logInfo("Creating table 'QUEUE' if not exist ...");
            obj_connection = getConnection();
            m_objLogManager.logInfo("Execute PRAGMA");
            try(Statement statement = obj_connection.createStatement()) {
                m_objLogManager.logInfo("going to Execute PRAGMA");
                statement.execute("PRAGMA temp_store_directory = 'D:\\SQLite-TMP';");
                m_objLogManager.logInfo("Execute PRAGMA successfully");
            }
            try(Statement statement = obj_connection.createStatement()) {
                m_objLogManager.logInfo("getting PRAGMA");
                try(ResultSet rs = statement.executeQuery("PRAGMA temp_store_directory;")) {
                    m_objLogManager.logInfo("getting PRAGMA successfull");
                    m_objLogManager.logInfo(rs.getString(1));
                }
            }
            obj_statement = obj_connection.createStatement();
            String str_sql = "CREATE TABLE IF NOT EXISTS QUEUE " +
                    "(Id VARCHAR(200) PRIMARY KEY     NOT NULL," +
                    " Origin            TEXT, " +
                    " Port              INT, " +
                    " RegisterAt        TEXT, " +
                    " UserName        TEXT)";
            obj_statement.executeUpdate(str_sql);
            m_objLogManager.logInfo("Table 'QUEUE' is created if not exist");
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (obj_statement != null) {
                obj_statement.close();
            }
            if (obj_connection != null) {
                obj_connection.close();
            }
        }

I also open my DB file in SQLite browser and execute following query
PRAGMA temp_store_directory = 'D:\SQLite-TMP';
But still SQLite creating temporary files in TEMP folder I want to change the location of temporary files into D:\SQLite-TMP this folder.
can anyone helped me as i am stuck in this problem, please let me know how to set the global variable sqlite3_temp_directory because I don't understand how to set it up from official documentation thanks.

My Environment

  • Windows 10
  • Sqlite database
  • JAVA 11

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩饰不了的爱 2025-02-16 17:12:46

首先,pragma temp_store_directory已弃用(请参阅

其次,sqlite_tmpdirtmpdir适用于unix like oss,看起来您在Windows上。

因此,我将尝试设置全局变量sqlite3_temp_directory,如上所述

希望这会有所帮助。

First, pragma temp_store_directory is deprecated (see this), so might not work anymore.

Second, SQLITE_TMPDIR and TMPDIR are for unix-like OSs, and looks like you're on Windows.

So I would try to set the global variable sqlite3_temp_directory as mentioned here at the bottom.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文