Skip to content

Commit e2433e6

Browse files
committed
Added python examples for how to read the DB file to tutorial.
1 parent 4e3580f commit e2433e6

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

docs/sqlite_readme.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The `fields` table contains the results of the file name check for every single
1919

2020
Within the `fields` table, your files are evaluated as follows (see [File Naming Convention](https://outerspace.stsci.edu/display/MASTDOCS/File+Naming+Convention) for more details):
2121

22-
- Captalization: the filename must be all lower case.
22+
- Capitalization: the filename must be all lower case.
2323
- Character Length: each field has a maximum character length.
2424
- Format: checks overall format and special characters: for example, a period `.` is allowed in the `<version>` field but not in the `<proj-id>`. Certain fields allow hyphen-separated elements. Most fields must begin and end with an ASCII alpha-numeric character.
2525
- Value: In some cases, the contents of each field are validated against known values to the extent possible.
@@ -31,3 +31,35 @@ Within the `filename` table (`Browse Data` tab, then select table `filename` in
3131
![DB Browser for SQLite after opening filename check DB file](../TUTORIAL/tutorial_images/DB_Browser_filename_Table_View.png "Figure 3")
3232

3333
Again, please see [`filename_check_readme.md`](../docs/filename_check_readme.md) for how to run the filechecker or the HLSP [File Naming Convention](https://outerspace.stsci.edu/display/MASTDOCS/File+Naming+Convention) for detailed rules.
34+
35+
### Opening the SQLite file with Python
36+
37+
An alternative way to read and interact with the DB file is to open it in Python. This might be useful for large HLSPs with many files, or if you want to explore the results programmatically.
38+
39+
For instance, if you want to get the full list of tables and views available to query, you can use the following Python code:
40+
41+
```python
42+
# load sqlite3 module
43+
import sqlite3
44+
45+
# modify the path to reflect the relative path of your results_mct-tutorial.db file
46+
dbfile = '/relative/path/to/results_mct-tutorial.db'
47+
48+
# create sql connection to db
49+
con = sqlite3.connect(dbfile)
50+
# create cursor that can be used to execute queries
51+
cur = con.cursor()
52+
53+
# query db for full list of tables and views
54+
table_list = [a for a in cur.execute("SELECT * from sqlite_master WHERE (type = 'table' or type = 'view')")]
55+
print(table_list) # should get '[('filename',), ('fields',), ('potential_problems',)]' as output
56+
57+
# close connection
58+
con.close()
59+
```
60+
61+
You can also execute queries within the potential_problems view to determine the number and severity of filename issues:
62+
63+
```python
64+
potential_problems = [a for a in cur.execute("SELECT * from potential_problems")]
65+
```

0 commit comments

Comments
 (0)