[SHARE CODE] PRAGMA query for sqlite
-
Ok, this is a stupid little class. It just attempts to extract all of the PRAGMA settings for given sqlite3 database. I say stupid, but all the SQLite utilities I have tried on iOS don't return all this information. There are some tricks for young players when doing this and I hope I have got it right. I wanted this class or functionality for the resource manager I am working on. So whilst it's not finished yet, I decided to share it anyway in case of some valuable comments. I list in the todo section what I still need to complete. Maybe this functionality is already built into SQLite somewhere and I will be embarrassed again for wasting time. But I did go into the standard libraries folder and opened up,all the files for SQLite. Nothing seemed to be hiding in there. Most of the time spent was on typing in the definitions and checking links etc....
But if it's useful to someone that would be good, if not I need it anyway.
Sorry, it's just in a gist at the moment. whilst it's a tiny class very wordy and long definitions, that's why it's better I don't paste it into here.
Gist link below...Hmmm, sorry, I have remove the gist link. There is an error in the code that I didn't see. I will repost once I can work it out 🤕
-
Do you get an error / exception or does it fail silently.
You might not have write access in an XCode created app. Can you
open('junk.txt', 'w')
in an XCode app?A few things to try out:
import os print('\n'.join(func(__file__) for func in (str, os.path.abspath, os.path.normcase, os.path.realpath))) print(os.path.relpath(__file__, os.curdir))
-
it was just saying could not create database...but I understand why now it was a failure of my path string- for some reason
filename = os.path.join(dir,'/sillllly.db')
was just returning '/sillllly.db.' instead of/Users/username/Library/Developer/CoreSimulator/Devices/randomalphanumericstrings/data/containers/Data/Application/more-alphan-numeric-mumbo-jumbo/Documents/sillllly.db
...Now I got it to work using
diry = os.path.dirname(__file__) combinedViaString = diry+"/sillllly.db"
and plugging that into connect()
Now, the above works, and the database says it's been created, and i can even create a table, however I can't see the database file in the main directory where my script is actually located. I guess this "/documents" area is some sort of temporary directory during runtime since that's not the actual directory I see in the Xcode file structure? or is it inaccessible to me?
And that really is my goal - to create a database file which i can see and have full control over in the file structure.
-
@Tizzy
os.path.join
treats both arguments as full paths. If the second one has a slash at the beginning, it is considered an absolute path (relative to the root directory/
), and the first path is effectively ignored. If you want to join two paths together, the second one must be a relative path. For exampleos.path.join("/usr/bin", "python")
returns"/usr/bin/python"
, butos.path.join("/usr/bin", "/python")
returns"/python"
.
-
@dgelessus said:
@Tizzy
os.path.join
treats both arguments as full paths. If the second one has a slash at the beginning, it is considered an absolute path (relative to the root directory/
), and the first path is effectively ignored. If you want to join two paths together, the second one must be a relative path. For exampleos.path.join("/usr/bin", "python")
returns"/usr/bin/python"
, butos.path.join("/usr/bin", "/python")
returns"/python"
.thank you I see what happened there then.
-
@ccc said:
You might not have write access in an XCode created app. Can you open('junk.txt', 'w') in an XCode app?
@ccc that open call did not throw any errors. I don't see any result for anything done, but no errors.
-
@Tizzy , did look though your dirs, to see if the db was created somewhere? It's really hard to get it to fail
-
The "temporary" directory during runtime I referred to above doesn't actually appear to be temporary because in subsequent runs, the table i created earlier is said to "already exist." However I can find NO trace of it. searching in xCode (the search tends to be pretty thorough there usually) and in finder, yields NOTHING.
Also worth noting, when run through the simulator all of those relative paths never once surfaced a "scripts" folder in the path, but a "Documents" folder. Scripts is the folder in which my scripts are in. But during runtime it doesn't know scripts exists.
FURTHER: just running the same thing outside of the simulator, the db file IS created and visible as it should be. So to conclude: iOS apps create a different directory structure at runtime, and I can't figure out how to manually access that outside of runtime, but if I pre-create the file will it get moved to the "runtime" "Documents" directory like all the other scripts so I can have a db file initialized before runtime but accessible during runtime using the relative directory stuff?
Does any of that make sense to you guys? I think this might work, initializing it outside of Xcode with the values i need, and then it should (i think) be put into the runtime directory along with the other files. I will report back with further findings.
-
@Tizzy , again I cannot help. But when searching for the database using the finder did you use wild cards, or use the filters for created today etc, also checking if it's visible. If the table reports it exisits , it must be there somewhere
-
I did find it at the absolute url specified in userr/Library/......./
But my takeaway is if you want to setup a sqlite database for use by an Xcode app, do so outside of Xcode if you want it to be in your scripts directory permanently.
-
Ok, I was just trying to test how big a empty database is... Now, I see an issue I was having before. Not sure why. But if you use sqlite3.connect() to create a database file and nothing else, it ends up as zero bytes. A valid sqlite3 db file is suppose to have a valid file header of a 100 bytes.
I did different ways, using with, committing then closing etc. but still made a zero byte file. Maybe this behaviour is expected, but not by me.
So my point being, if you make a db file as a placeholder. Good to check it's a valid sqlite3 db file
-
@Phuket2 said:
Ok, I was just trying to test how big a empty database is... Now, I see an issue I was having before. Not sure why. But if you use sqlite3.connect() to create a database file and nothing else, it ends up as zero bytes. A valid sqlite3 db file is suppose to have a valid file header of a 100 bytes.
I did different ways, using with, committing then closing etc. but still made a zero byte file. Maybe this behaviour is expected, but not by me.
So my point being, if you make a db file as a placeholder. Good to check it's a valid sqlite3 db fileI can confirm a newly created .db sqlite3 file is 0Kb. adding a table makes it 8Kb.
-
@Tizzy , but I am glad I tested it. This has being giving me grief. I didn't know why. Now I know why ðŸ˜ðŸ˜±