import sqlite3


def query_db(dbfile, language="Python"):
  """
  Query kaggle database for scripts associated with a particular language
  :param dbfile: location of kaggle db
  :param language: language to query, default is Python. IPython Notebook retrieves notebooks instead.
  :returns: database cursor
  """
  conn = sqlite3.connect(dbfile)
  c = conn.cursor()
  py_scripts_query = f"""
       select 
         Scripts.AuthorUserId as user_id,
         Scripts.ScriptProjectId AS project_id,
         ScriptVersions.id as script_id,
         Scripts.ForkParentScriptVersionId as parent_id,
         ScriptVersions.ScriptContent as script
      from ScriptVersions, Scripts
        where Scripts.CurrentScriptVersionId = ScriptVersions.Id
        and Scripts.Id IS NOT NULL
        and ScriptVersions.ScriptLanguageId = (select Id from ScriptLanguages where Name = "{language}")
        group by ScriptContent"""
  print('Querying db...')
  c.execute(py_scripts_query)
  return c