When Oracle inserts string data, there is a ' single quote ' in the string

Source: Internet
Author: User

Use INSERT INTO (field1,field2 ...) VALUES (' Val1 ', ' val2 ' ...) , an error occurs if there is a single quotation mark in the value.

Processing method: Determine whether the Val1,val2 contains single quotation marks, if the single quotation marks, then the single quotation mark ' replaced with two single quotation marks '.

Organize fields and field values into a hashtable, and then abstract a function getsqlbyhashtable () that organizes 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 ();

}

When Oracle inserts string data, there is a ' single quote ' in the string

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.