GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Import CSV column order · Issue #243 · sqlitebrowser/sqlitebrowser

Import CSV column order #243

New issue
Copy link
New issue
Copy link
Open
Open
Import CSV column order#243
Copy link
Labels
enhancementFeature requests.Feature requests.import

Description

@galmok
galmok
opened on Mar 29, 2015
Issue body actions

It would be great if the column order during csv import could be changed. If the order of the columns in the target database doesn't match the source database, swapping the columns around manually is somewhat difficult.

Activity

  1. justinclift commented on Mar 30, 2015

    @justinclift
    justinclift
    on Mar 30, 2015
    Member
    More actions

    Hmmmm, needing to import columns in a different order than defined in a csv file is pretty unusual (I think anyway). I doubt we'll do this... but if someone sends in a patch to make it happen we'll happily merge it. 😄

  2. galmok commented on Mar 30, 2015

    @galmok
    galmok
    on Mar 30, 2015 via email
    Author
    More actions
    Well, the problem is that csv import ignores column names in the csv file and assumes the order is the same. In my situation this was not so. I resorted to fixing the sql export as fixing the csv file was too much work. But as a csv importer is already present, a dialog allowing the user to arrange the csv columns before import would make sense.
  3. jungle-boogie commented on May 16, 2016

    @jungle-boogie
    jungle-boogie
    on May 16, 2016
    Contributor
    More actions

    I'm able to change the column order during a csv import in the latest master.

  4. justinclift commented on Jan 28, 2017

    @justinclift
    justinclift
    on Jan 28, 2017
    Member
    More actions

    Looking back over this... @jungle-boogie how did you rearrange the column order during import?

  5. jungle-boogie commented on Jan 30, 2017

    @jungle-boogie
    jungle-boogie
    on Jan 30, 2017
    Contributor
    More actions

    @justinclift,

    I think I'm mistaken.

    I think what I was saying is that within a table I can change the column order, but I cannot change a table order of an import. Sorry about that.

    best,
    jb

  6. justinclift
    added
    enhancementFeature requests.Feature requests.
    on May 7, 2017
  7. aneroid commented on Jul 30, 2017

    @aneroid
    aneroid
    on Jul 30, 2017
    More actions

    Was about to post a new issue and then I see this one exists and is still open.

    Details for the issue

    When importing data from a CSV file into an existing table, the column names in header row are ignored and the import occurs only on column order. So if the source data has columns out of order, then the data is goes into the wrong columns.

    If importing a CSV file as a new table, then it uses the header row as the column names and in that order.

    If this behavior is by design and not a bug - it is inconsistent wrt import as new table vs into existing table.

    (Tested on 3.9.1 and 3.10.0-beta1-win64.)

  8. aneroid commented on Jul 30, 2017

    @aneroid
    aneroid
    on Jul 30, 2017
    More actions

    @justinclift Wrt

    Hmmmm, needing to import columns in a different order than defined in a csv file is pretty unusual (I think anyway).

    Maybe switching the column order during import is more work than it's worth - but reading the header row (if the user leaves the box checked) and then matching that with the table's column names, if the table exists, should be straightforward. Especially if the header row names are a 100% match against the table's column names.

    And providing a header-to-column name dialog when importing into an existing table may be easier than column switching.

    I finally used something like this to do the imports in Python:

    with open('some_file.csv', 'Urb') as csvfile:
        reader = csv.DictReader(csvfile, dialect='excel')
        fieldnames = reader.fieldnames  # DictReader uses the header row for dict keys
        insert_query = u"""
            INSERT OR IGNORE INTO `some_table`
            ({})
            VALUES
            ({})
            """.format(', '.join('`%s`' % f for f in fieldnames),  # column names
                       ', '.join(':%s' % f for f in fieldnames))  # named parameters
            # the insert query construction maps the fieldnames to columns with the same name
        for row in reader:
            # in actual version, I optimised by inserting 10,000 rows at a time before committing
            row = {k.decode('utf-8'): v.decode('utf-8') for k, v in row.items()}
            _ = cursor_lite.execute(insert_query, row)
            # when `row` is a dict/mapping and not a list/tuple,
            # then it's matched on param names and keys

    This inserts on names rather than order and skips fieldnames in the csv which are not columns of the table. (If a not-null column is missed, that should error out anyway.)

    I doubt we'll do this... but if someone sends in a patch to make it happen we'll happily merge it. 😄

    Alas, my C++ skills are just high-school level from too many years ago.

  9. justinclift commented on Jul 30, 2017

    @justinclift
    justinclift
    on Jul 30, 2017
    Member
    More actions

    If this behavior is by design and not a bug

    Hmmm, it's kind of neither. It's just that when this was first implemented, the concept of "lets do a match up by column name" probably didn't spring to mind. So, wasn't part of how things were put in place.

    That being said, it's not a bad idea. It would be useful for some scenario's and there's no reason to not implement this.

    @iKlsR Any interest in getting this done? 😄

    Reacted by Anirudh Dutt
  10. aneroid commented on Jul 30, 2017

    @aneroid
    aneroid
    on Jul 30, 2017
    More actions

    FWIW, "match csv Headers to Column names" should be the default when the user selects 'headers in first row'. However, for those who actually want to import by order and ignore the header row - that may break backwards behaviour. Probably add a checkbox for "match headers & columns" which is enabled (and checked) only if 'headers in first row' is checked.

  11. justinclift commented on Jul 30, 2017

    @justinclift
    justinclift
    on Jul 30, 2017
    Member
    More actions

    Yeah, when thinking about this a bit more earlier on today... I can't really imagine a scenario where someone supplies a csv with column headers and doesn't want the columns matching up with the same column names in an existing table.

    eg not real sure a checkbox selector is needed

    If course, I might just be not thinking of some super obvious scenario... 😄

  12. iKlsR commented on Jul 31, 2017

    @iKlsR
    iKlsR
    on Jul 31, 2017
    Member
    More actions

    Sure, I won't be able to get to it for a couple weeks however.

  13. justinclift
    assigned
    iKlsR
    on Jul 31, 2017
  14. mgrojo
    added
    import
    on Oct 4, 2018
  15. iKlsR
    unassigned
    iKlsR
    on Nov 17, 2019
  16. FriedrichFroebel commented on Feb 26, 2021

    @FriedrichFroebel
    FriedrichFroebel
    on Feb 26, 2021
    Contributor
    More actions

    I stumbled upon this as well today when trying to import some psql data dump into a SQLite database for testing purposes. Writing the corresponding UPDATE query manually is not really a problem, but I would not have expected this behaviour with the headers option enabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementFeature requests.Feature requests.import

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.