ray
ray

Reputation: 4250

Store special characters in database

I have an upload function in JAVA to import data using CSV file into a postgresql 9.2 database. Currently the data is:

ID, DESCRIPTION, PRICE, EOR
1001, "Lenovo Laptop" 20% Discount @ Jackson's Street, 500$, EOR
1002, "Samsung Laptop" 10% Discount @ Jackson's Street, 440.90$, EOR

string description = line[1];

Now when I am trying to store DESCRIPTION field value into database it's throwing error. Is there anyway that I can store this data into database exactly how it's written in CSV file?

Upvotes: 2

Views: 782

Answers (2)

zaz
zaz

Reputation: 970

Normally it shouldn't throw any error during upload. If really it's throwing any error due to special character it's only for the reason mentioned by Craig Ringer. You can avoid this by creating customized function like following:

removeSpecialCharacter(String inputStr) {
   inputStr = inputStr.replace('"', '');
}

But you may face problem to pull this special characters from database and use it. If you want to show these special characters in browser you need to use escaping method for special character. Details procedure of escaping special character is here.

Upvotes: 1

Craig Ringer
Craig Ringer

Reputation: 324901

That's weird pseudo-CSV. Usually a record containing double-quotes will escape them or wrap them in another set of quotes around the whole record.

You might need to override the quote character in the copy command so Pg doesn't interpret the double-quotes as CSV quoting.

Upvotes: 2

Related Questions