Created
July 15, 2014 01:48
-
-
Save t33chong/4baa0171fb4bce6e9955 to your computer and use it in GitHub Desktop.
Is there a way to avoid having to select in order to get the UID after inserting a new row?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_location_uid(name, country_code): | |
| # TODO: DRY, figure out a better way to do this | |
| cursor.execute("SELECT uid FROM location WHERE name = '%s' AND country_code = '%s'" % (name, country_code)) | |
| result = cursor.fetchone() | |
| if result is not None: | |
| return result[0] | |
| cursor.execute("INSERT INTO location (name, country_code) VALUES ('%s', '%s')" % (name, country_code)) | |
| db.commit() | |
| cursor.execute("SELECT uid FROM location WHERE name = '%s' AND country_code = '%s'" % (name, country_code)) | |
| result = cursor.fetchone() | |
| return result[0] |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the schema for the location table: