Beispiel #1
0
def drop_table():
    print 'Dropping courses table...'
    pg = pg_sync()
    db_query = 'DROP TABLE courses_t;'
    cursor = pg.cursor()
    cursor.execute(db_query)
    pg.commit()
Beispiel #2
0
def load_data_from_json(json_data):
    pg = pg_sync()
    query_queue = []
    for course in json_data:
        pairs = [(name, _typify(course[name], data_type)) for (name,
                data_type) in schema if name not in special_fields]
        pairs += _special_treatment(course, schema)
        [columns, values] = zip(*pairs)
        db_query = 'INSERT INTO courses_t (%s) VALUES (%s);' % (
                ', '.join(columns), ', '.join(["%s"] * len(values)))
        query_queue.append(values)
        if len(query_queue) == 1000:
            print 'submitting a batch'
            cursor = pg.cursor()
            cursor.executemany(db_query, query_queue)
            pg.commit()
            cursor.close()
            query_queue = []
    if query_queue:
        print 'submitting a batch'
        cursor = pg.cursor()
        cursor.executemany(db_query, query_queue)
        pg.commit()
        cursor.close()
        query_queue = []
Beispiel #3
0
def create_table():
    print 'Creating courses table with proper schema...'
    pg = pg_sync()
    db_query = 'CREATE TABLE IF NOT EXISTS courses_t (%s);' % (", ".join(
            ['%s %s' % column for column in schema]))
    cursor = pg.cursor()
    cursor.execute(db_query)
    pg.commit()
Beispiel #4
0
def query_database(**kwargs):
    pg = pg_sync()
    recognized_arguments = valid_query_arguments()
    queries = get_recognized_arguments(recognized_arguments, **kwargs)
    if not queries:
        return error(status_code=400, status_txt="MISSING_QUERY_ARGUMENTS")
    query, arguments = build_sql_query(queries)
    cursor = pg.cursor()
    cursor.execute(query, arguments)
    return on_sql_response(cursor)