# Creating temporary database, adding and loading Dicoms

**URL:** <https://discourse.slicer.org/t/creating-temporary-database-adding-and-loading-dicoms/12521>\
**Category:** Support\
**Created:** [July 14, 2020, 7:05am UTC](https://discourse.slicer.org/t/creating-temporary-database-adding-and-loading-dicoms/12521 "2020-07-14T07:05:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sogo](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@sogo](https://discourse.slicer.org/u/sogo)\
**Post date:** [July 14, 2020, 7:05am UTC](https://discourse.slicer.org/t/creating-temporary-database-adding-and-loading-dicoms/12521/1 "2020-07-14T07:05:32Z")

</div>

I have a question regarding creating temporary database, I got the script from ‘ScriptRepository’ of creating temporary database and loading DICOMs to slicer " **How to load DICOM files into the scene from a folder**" . My issue is that when I populates the database using “importDicom” function, the database is still empty. I’m working on c++ loadable module in which I want to load DICOM files into temporary database and as loader is python based module, I have to process python script in c++. Here is what this code looks like:

> PythonQt::init();  
> PythonQtObjectPtr context = PythonQt::self()-\>getMainModule();  
> context.addVariable(“folderName”, d-\>folderName);  
> context.evalScript(QString(“import os,sys,slicer,random\n”  
> “from pathlib import Path\n”  
> “dicomDataDir=folderName\n”  
> “directory=‘TEMP’\n”  
> “from DICOMLib import DICOMUtils\n”  
> “with DICOMUtils.TemporaryDICOMDatabase(directory) as db:\n”  
> " DICOMUtils.clearDatabase(db)\n"  
> " DICOMUtils.importDicom(dicomDataDir, db)\n"  
> " patientUIDs = db.patients()\n"  
> " for patientUID in patientUIDs:\n"  
> " DICOMUtils.loadPatientByUID(patientUID)"));

The above code is called on click of button and “folderName” variable has all the dicom files. When this evalScript is done, in separate function in c++, I’m opening the database using following code but it shows empty output for patientsUIDs:

> ```
> ctkDICOMDatabase *data;
> data->openDatabase("/tmp/SlicerCAT-user/TEMP/ctkDICOM.sql"); //This is where temporary database is
> QStringList patients = data->patients();
> qDebug() << "Patients UIDs" << patients;
> qDebug() << "StudyList" << data->studiesForPatient(patients[0]);
> 
> ```

My question is that does the temporary database is cleared automatically when “evalScript” is run and finishes. During evalScript, I printed the studyList of loaded Dicoms and it shows correct output.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 14, 2020, 1:41pm UTC](https://discourse.slicer.org/t/creating-temporary-database-adding-and-loading-dicoms/12521/2 "2020-07-14T13:41:59Z")

</div>

Loading data into Slicer’s persistent DICOM database is even simpler: replace

```
with DICOMUtils.TemporaryDICOMDatabase() as db

```

by this:

```
db = slicer.dicomDatabase

```

Alternatively, you can use this code snippet to import:  
[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How\_to\_import\_DICOM\_files\_into\_the\_application.27s\_DICOM\_database](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_import_DICOM_files_into_the_application.27s_DICOM_database)

---

<div class="post-metadata">

**Author:** ![sogo](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@sogo](https://discourse.slicer.org/u/sogo)\
**Post date:** [July 15, 2020, 11:24pm UTC](https://discourse.slicer.org/t/creating-temporary-database-adding-and-loading-dicoms/12521/3 "2020-07-15T23:24:57Z")

</div>

So there is no definite way to make persistent temporary database. I think temporary is not a good word to describe what I’m looking for. I am looking for a separate database for my application at my given location directory, separate from slicer’s default database. I was thinking of creating temporary database and using ctkDicomIndexer to populate and ctkDicomDatabase to open it. Sorry for late response, I was just figuring somethings out

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 15, 2020, 11:55pm UTC](https://discourse.slicer.org/t/creating-temporary-database-adding-and-loading-dicoms/12521/4 "2020-07-15T23:55:33Z")

</div>

You can pick any database directory you want and tell Slicer to use it. If you want the user to have their own config, you just need to remember to set if back if to their setting after they use your module. The temporary dicom database utilities are handy because they do that cleanup for you automatically after a transient operation.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 14, 2023, 7:56pm UTC](https://discourse.slicer.org/t/creating-temporary-database-adding-and-loading-dicoms/12521/6 "2023-09-14T19:56:58Z")

</div>

A post was split to a new topic: [Finding a DICOM series based on series description](https://discourse.slicer.org/t/finding-a-dicom-series-based-on-series-description/31726)
