Last active
November 19, 2019 21:54
-
-
Save f6v/0d9e6276a338e9664107da70ca9d3901 to your computer and use it in GitHub Desktop.
Read SQLite database
This file contains 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
import sqlite3 | |
import sys | |
def get_gene_ids(gene_symbol): | |
try: | |
# estabilish a SQLIte connection from Python | |
sqlite_connection = sqlite3.connect('refGene_hg19.db') | |
# Better formatting for results | |
sqlite_connection.row_factory = lambda cursor, row: row[0] | |
# create a cursor object using the connection object | |
cursor = sqlite_connection.cursor() | |
print('Successfully Connected to SQLite.') | |
# define SQLite SELECT statement query | |
sqlite_select_query = "SELECT bin from refGene where name2 = (?)" | |
# execute the SELECT query by bring the cursor.execute method | |
cursor.execute(sqlite_select_query, (gene_symbol,)) | |
# use cursor.fetchall method to select a row | |
return cursor.fetchall() | |
except sqlite3.Error as error: | |
print("Fail to ?", error) | |
finally: | |
if(sqlite_connection): | |
sqlite_connection.close() | |
print("The SQLite connection is closed.") | |
input_gene_symbol = sys.argv[1] | |
gene_ids = get_gene_ids(input_gene_symbol) | |
if len(gene_ids) > 0: | |
print("DB has following gene ids: ", *gene_ids) | |
else: | |
print("No results") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment