# LibPQ copy error when field of the data frame contains comma

**URL:** https://discourse.julialang.org/t/libpq-copy-error-when-field-of-the-data-frame-contains-comma/94058
**Category:** Data
**Created:** [February 4, 2023, 5:49pm UTC](https://discourse.julialang.org/t/libpq-copy-error-when-field-of-the-data-frame-contains-comma/94058 "2023-02-04T17:49:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ultratrader](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ultratrader/32/42836_2.png) [@ultratrader](https://discourse.julialang.org/u/ultratrader)
#### Post date: [February 4, 2023, 5:49pm UTC](https://discourse.julialang.org/t/libpq-copy-error-when-field-of-the-data-frame-contains-comma/94058/1 "2023-02-04T17:49:12Z")

</div>

I am trying to load data from data frame to postgresql using LibPQ.jl. it work quite fast until it reachs field that contains “,” such as “Oil, Gas & Coal” and throw an error:

```julia
[error | LibPQ]: BadCopyFileFormat: ERROR: extra data after last expected column
CONTEXT: COPY table_test, line 2: "ADMR,Adaro Minerals Indonesia Tbk.,Energy, Oil, Gas & Coal"

```

I use below code, adapted from [here](https://invenia.github.io/LibPQ.jl/stable/)

```julia
using LibPQ, DataFrames
conn = LibPQ.Connection("host=/var/run/postgresql/ dbname=dbtest")

row_upload = map(eachrow(df2)) do row
    if ismissing(row[:stock_id])
        "$(row[:stock_id]),\n"
    else
        "$(row[:stock_id]),$(row[:company]),$(row[:sector]),$(row[:subsector])\n"
    end
end

copyin = LibPQ.CopyIn("COPY table_test FROM STDIN (FORMAT CSV);", row_upload)

execute(conn, copyin)

close(conn)

```

I have test it by separating some part of the data frame and every thing is OK when there is no “,” in each field of the data frame.  
honestly, I am quite new to programming, any suggestion how to solve it efficiently without removing the “,” from the table?

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [February 4, 2023, 6:14pm UTC](https://discourse.julialang.org/t/libpq-copy-error-when-field-of-the-data-frame-contains-comma/94058/2 "2023-02-04T18:14:04Z")

</div>

CC @quinnj

---

<div class="post-metadata">

### Author: ![iamed2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamed2/32/215082_2.png) [@iamed2](https://discourse.julialang.org/u/iamed2)
#### Post date: [March 13, 2023, 6:11pm UTC](https://discourse.julialang.org/t/libpq-copy-error-when-field-of-the-data-frame-contains-comma/94058/3 "2023-03-13T18:11:56Z")

</div>

You’ll want to quote any fields that could contain commas. You can also do this when this is not necessary, so you could have this to be safe:

```julia
"\"$(row[:stock_id])\",\"$(row[:company])\",\"$(row[:sector])\",\"$(row[:subsector])\"\n"

```
