module sql.database_helper

Short summary

module pyensae.sql.database_helper

Contains functions to import a text file into a database (SQLite).

source on GitHub

Functions

function

truncated documentation

import_flatfile_into_database

Function which imports a file into a database. It the table exists, it removes it first. There is no addition.

Documentation

Contains functions to import a text file into a database (SQLite).

source on GitHub

pyensae.sql.database_helper.import_flatfile_into_database(filedb, filetext, table=None, header=True, columns=None, engine='SQLite', host='localhost', add_key=None, encoding='utf-8', fLOG=<built-in function print>)

Function which imports a file into a database. It the table exists, it removes it first. There is no addition.

Parameters:
  • filedb – something.db3

  • filetext – something.txt or .tsv

  • table – table name (in the database), if None, the database name will be the filename without extension

  • columns – if header is False, this must be specified. It should be a list of column names.

  • header – boolean (does it have a header or not)

  • engine – engine to use when using a SQL server (SQLite or ODBCMSSQL)

  • host – host (server)

  • fLOG – logging function (will display information through the command line)

  • add_key – name of a key to add (or None if nothing to add)

  • encoding – encoding

Returns:

table name

Import a flat file into a SQLite database

from pyensae import import_flatfile_into_database
dbf = "database.db3"
file = "textfile.txt"
import_flatfile_into_database(dbf, file)

On Windows, SQLiteSpy is a free tool very useful to run SQL queries against a sqlite3 database.

source on GitHub