Generate insert SQL statements from a CSV file

后端 未结 12 2047
轮回少年
轮回少年 2020-12-04 16:07

I need to import a csv file into Firebird and I\'ve spent a couple of hours trying out some tools and none fit my needs.

The main problem is that al

相关标签:
12条回答
  • 2020-12-04 16:50

    A tool I recently tried that worked outstandingly well is FSQL.

    You write an IMPORT command, paste it into FSQL and it imports the CSV file into the Firebird table.

    0 讨论(0)
  • 2020-12-04 16:51

    It's a bit crude - but for one off jobs, I sometimes use Excel.

    If you import the CSV file into Excel, you can create a formula which creates an INSERT statement by using string concatenation in the formula. So - if your CSV file has 3 columns that appear in columns A, B, and C in Excel, you could write a formula like...

    ="INSERT INTO MyTable (Col1, Col2, Col3) VALUES (" & A1 & ", " & B1 & ", " & C1 & ")"
    

    Then you can replicate the formula down all of your rows, and copy, and paste the answer into a text file to run against your database.

    Like I say - it's crude - but it can be quite a 'quick and dirty' way of getting a job done!

    0 讨论(0)
  • 2020-12-04 16:52

    you can use shell

    sed "s/,/','/g" file.csv > tmp
    sed "s/$/'),(/g" tmp > tmp2
    sed "s/^./'&/g" tmp2 > insert.sql
    

    and then add

    INSERT INTO PERSON (ID, NAME, CITY_ID) VALUES(
    ...
    );
    
    0 讨论(0)
  • 2020-12-04 16:55

    You can use the free csvsql to do this.

    • Install it using these instructions
    • Now run a command like so to import your data into your database. More details at the links above, but it'd be something like:

      csvsql --db firebase:///d=mydb --insert mydata.csv

    • The following works with sqlite, and is what I use to convert data into an easy to query format

      csvsql --db sqlite:///dump.db --insert mydata.csv

    0 讨论(0)
  • 2020-12-04 16:58

    Just finished this VBA script which might be handy for this purpose. All should need to do is change the Insert statement to include the table in question and the list of columns (obviously in the same sequence they appear on the Excel file).

    Function CreateInsertStatement()
        'Output file location and start of the insert statement
        SQLScript = "C:\Inserts.sql"
        cStart = "Insert Into Holidays (HOLIDAY_ID, NAT_HOLDAY_DESC, NAT_HOLDAY_DTE) Values ("
    
        'Open file for output
        Open SQLScript For Output As #1
    
        Dim LoopThruRows As Boolean
        Dim LoopThruCols As Boolean
    
    
        nCommit = 1 'Commit Count
        nCommitCount = 100 'The number of rows after which a commit is performed
    
        LoopThruRows = True
        nRow = 1 'Current row
    
        While LoopThruRows
    
            nRow = nRow + 1 'Start at second row - presuming there are headers
            nCol = 1 'Reset the columns
            If Cells(nRow, nCol).Value = Empty Then
                Print #1, "Commit;"
                LoopThruRows = False
            Else
                If nCommit = nCommitCount Then
                    Print #1, "Commit;"
                    nCommit = 1
                Else
                    nCommit = nCommit + 1
                End If
    
                cLine = cStart
                LoopThruCols = True
    
                While LoopThruCols
                    If Cells(nRow, nCol).Value = Empty Then
                        cLine = cLine & ");"                    'Close the SQL statement
                        Print #1, cLine                         'Write the line
                        LoopThruCols = False                    'Exit the cols loop
                    Else
                        If nCol > 1 Then                        'add a preceeding comma for all bar the first column
                            cLine = cLine & ", "
                        End If
                        If Right(Left(Cells(nRow, nCol).Value, 3), 1) = "/" Then 'Format for dates
                            cLine = cLine & "TO_DATE('" & Cells(nRow, nCol).Value & "', 'dd/mm/yyyy')"
                        ElseIf IsNumeric(Left(Cells(nRow, nCol).Value, 1)) Then 'Format for numbers
                            cLine = cLine & Cells(nRow, nCol).Value
                        Else 'Format for text, including apostrophes
                            cLine = cLine & "'" & Replace(Cells(nRow, nCol).Value, "'", "''") & "'"
                        End If
    
                        nCol = nCol + 1
                    End If
                Wend
            End If
        Wend
    
        Close #1
    
    End Function
    
    0 讨论(0)
  • 2020-12-04 16:59

    Two online tools which helped me in 2020:

    https://numidian.io/convert/csv/to/sql

    https://www.convertcsv.com/csv-to-sql.htm

    The second one is based on JS and does not upload your data (at least not at the time I am writing this)

    0 讨论(0)
提交回复
热议问题