When oracle inserts string data, the string contains & #39; single quotation marks, oracle string

Source: Internet
Author: User

When oracle inserts string data, the string contains 'single quotes, oracle strings

When you use insert into (field1, field2. ..) values ('val1', 'val2'...), an error is returned if the value contains single quotes.

Solution: Check whether val1 and val2 contain single quotation marks. If single quotation marks are included, replace single quotation marks with two single quotation marks ''.

Organize fields and field values into a HashTable, and then abstract the function getSqlByHashTable () for organizing SQL statements ():

HashTable ht = new HashTable ();

Ht. add (field1, val1 );

Ht. add (field2, val2 );

Ht. add (field3, val3 );

...

Public string getSqlByHashTable (string tablename, HashTable ht ){

StringBuilder sb = new StringBuilder ();

Sb. append ("insert into" + tablename + "(");

StringBuilder fsb = new StringBuilder ();

StringBuilder vsb = new StringBuilder ();

Foreach (var key in ht. Keys)

{

Fsb. append (key + ",");

String value = ht [key];

If (value. trim () = "")

{

Value = "";

}

Else

{

If (value. Contains ("'"))

{

Value = value. replace ("'","''");

}

Value = "'" + value + "'";

}

Vsb. append (value + ",");

}

Sb. append (fsb. toString (). Substring (0, fsb. toString (). length-1 ));

Sb. append (") values (");

Sb. append (vsb. toString (). Substring (0, vsb. toString (). length-1 ));

Sb. append (")");

 

Return sb. toString ();

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.