Retrieve all table names from existing SQLite file. - #72
Conversation
| def print_tablenames(filename): | ||
| """print tablenames and return them as list. Returns empty list if file does not exist (instead of creating that file)""" | ||
| if not os.path.isfile(filename): | ||
| logger.warning('file %s does not exist' %filename) |
There was a problem hiding this comment.
Explicit fail (exception) better?
Also, spaces around %.
| if not os.path.isfile(filename): | ||
| logger.warning('file %s does not exist' %filename) | ||
| return | ||
| raise IOError('file %s does not exist' % (filename)) |
There was a problem hiding this comment.
What does sqlite3 do when asked to connect to a file that doesn't exist? Isn't that "default" error better?
There was a problem hiding this comment.
it's weird. It creates a database of that name and returns an empty list
There was a problem hiding this comment.
I see. That's really bad behaviour from sqlite3 -- I agree raising an exception is much better.
|
As proposal, instead of printing tables maybe it will be better to return the list of them and user will decide how to use this list? |
menshikh-iv
left a comment
There was a problem hiding this comment.
Good start, don't forget about tests (need to add tests for new functionality)
| conn = sqlite3.connect(filename) | ||
| res = conn.execute("SELECT name FROM sqlite_master WHERE type='table';") | ||
| tablenames = [name[0] for name in res] | ||
| print(tablenames) |
There was a problem hiding this comment.
return tablenames enough (no need to print it)
| return loads(bytes(obj)) | ||
|
|
||
|
|
||
| def print_tablenames(filename): |
There was a problem hiding this comment.
IMO better to move it to SqliteDict class and use as method, wdyt @transfluxus ?
There was a problem hiding this comment.
possible. but that would create already at least one table (unnamed). could be a static method. The way use it is to cal it before creating the SqliteDict obj.
There was a problem hiding this comment.
But what for? I don't see a problem here.
|
instead of this: I would rather have something like this: |
|
but just let me know, what you prefer. it includes both versions now |
|
|
||
| def get_tablenames(self): | ||
| """print tablenames and return them as list""" | ||
| res = self.conn.select("SELECT name FROM sqlite_master WHERE type='table';") |
There was a problem hiding this comment.
-1 on code duplication, it makes maintenance a headache. Keep it DRY.
Can you refactor the core functionality to live in only one place? Possibly being used/called from multiple places, that's fine.
|
Of course. That's what I came up with in the end, since the db connection/cursor are in the run of the multithread obj. I'm not used to write test :), but I added some. Hope they make sense |
| @@ -0,0 +1,6 @@ | |||
| { | |||
There was a problem hiding this comment.
unrelated file, please remove
There was a problem hiding this comment.
aii. sneaked in...
| self.assertEqual(db1.get_tablenames(), ['table1']) | ||
| db2 = sqlitedict.SqliteDict(fname,tablename='table2') | ||
| self.assertEqual(db1.get_tablenames(), ['table1','table2']) | ||
| db1.close() |
There was a problem hiding this comment.
with SliteDict(...):
self.assertEqual
better than db = ; db.close()
There was a problem hiding this comment.
is there a reason you're not having
from sqlitedict import SliteDict ?
There was a problem hiding this comment.
ok, I did, makes things look cleaner.
having with makes things look better indeed. However my test is now the only one doing that. looks a bit inconsistent...
There was a problem hiding this comment.
@transfluxus I think we'll cleanup codebase later, don't worry :)
|
|
||
| def test_tablenames(self): | ||
| fname = norm_file('tests/db/tablenames-test-1.sqlite') | ||
| db = sqlitedict.SqliteDict(fname) |
There was a problem hiding this comment.
what's about an empty file?
There was a problem hiding this comment.
not sure what you mean. in any case table unnamed will be created
| return loads(bytes(obj)) | ||
|
|
||
|
|
||
| def get_tablenames(filename): |
There was a problem hiding this comment.
probably better to remove this function & move all code to method, wdyt?
There was a problem hiding this comment.
you mean just a class method?
I can do that. just makes use case look unpractical. see above
- remove temp ipynb
adjust tests
| yield rec | ||
|
|
||
| def select_one(self, req, arg=None): | ||
| def select_one(self, req, arg=None): |
| from sys import version_info | ||
| major_version = version_info[0] | ||
|
|
||
| from accessories import norm_file |
| """get the names of the tables in an sqlite db as a list""" | ||
| if not os.path.isfile(filename): | ||
| raise IOError('file %s does not exist' % (filename)) | ||
| conn = sqlite3.connect(filename) |
There was a problem hiding this comment.
what's about with here too?
change test imports
|
Thank you @transfluxus! Congratz with first contribution 🥇 |
Add
get_tablenamesutility method that retrieves all table names from the file with SQLite database.FIxes #71.