Skip to content

CGATcore CSV2DB Module

CSV2DB.py - utilities for uploading a table to database

:Tags: Python

Purpose

create a table from a csv separated file and load data into it.

This module supports backends for postgres and sqlite3. Column types are auto-detected.

.. todo::

Use file import where appropriate to speed up loading. Currently, this is not always the case.

Usage

Documentation

Code

to_sql_pkey(self, frame, name, if_exists='fail', index=True, index_label=None, schema=None, dtype=None, **kwargs)

Function to load a table with the reqirement for a primary key.

Source code in cgatcore/csv2db.py
def to_sql_pkey(self, frame, name, if_exists='fail', index=True,
                index_label=None, schema=None,
                dtype=None, **kwargs):
    '''Function to load a table with the reqirement for a primary key.'''
    if dtype is not None:
        from sqlalchemy.types import to_instance, TypeEngine
        for col, my_type in dtype.items():
            if not isinstance(to_instance(my_type), TypeEngine):
                raise ValueError('The type of %s is not a SQLAlchemy '
                                 'type ' % col)

    table = pandas.io.sql.SQLTable(name, self,
                                   frame=frame,
                                   index=index,
                                   if_exists=if_exists,
                                   index_label=index_label,
                                   schema=schema,
                                   dtype=dtype, **kwargs)
    table.create()
    table.insert()