def initialize_database(self, connection):
        cursor = connection.cursor()

        try:

            # create a metadata table to track version and custom keys/values
            sql = 'create table metadata (key integer primary key, value integer, value_bin blob)'
            cursor.execute(sql)

            # set the database version
            sql = 'insert into metadata (key, value, value_bin) values (?, ?, ?)'
            cursor.execute(sql, (KEY_VERSION, self.Version, None))

            # create table statement
            column_defs = ','.join("%s %s" % (n, t) for (n, t, i) in self.Columns)
            sql = 'create table %s (%s)' % (self.Name, column_defs)
            cursor.execute(sql)

            # add each index
            for (n, t, i) in self.Columns:
                if not i: continue

                sql = "create index index_%s on %s (%s)" % (n, self.Name, n)
                cursor.execute(sql)

            # let subclasses pre-populate the database
            self.populate_database(cursor)

            connection.commit()

        # this will happen every time except the first time
        except sqlite3.OperationalError, e:
            if e.message != ('table metadata already exists'):
                raise e

            # check the version is compatible
            version = self.get_metadata(cursor, KEY_VERSION)
            if version != self.Version:
                raise DatabaseException('incompatible database version: %d (expected %d)' % (version, self.Version))
dataset/ETHPy150Open ricmoo/pycoind/pycoind/blockchain/database.py/Database.initialize_database/original