How to insert a string using C interface that contains both single and double quotes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to insert a string using C interface that contains both single and double quotes

Sankara Narayanan
Hi,

I have a tricky situation where I have strings in my data that have both
single and double quotes. I am using the C APIs of sqlite3_prepare and
sqlite3_exec to insert data into the database. How do I prepare the query
string in such case where the string data have both single and double
quotes.

FYI, The string data are some meta data information and hence they can
contain all possible characters with any possible combination and there is
not restriction for special characters.

Your suggestions are highly appreciated.

Thank you,

With regards,
Sankara Narayanan B.
Reply | Threaded
Open this post in threaded view
|

Re: How to insert a string using C interface that contains both single and double quotes

Sergey Startsev
Hello,

Wednesday, May 25, 2005, 11:30:00 PM, you wrote:

SN> single and double quotes. I am using the C APIs of sqlite3_prepare and
SN> sqlite3_exec to insert data into the database. How do I prepare the query
SN> string in such case where the string data have both single and double
SN> quotes.

This insert works currently:

INSERT INTO test (str)
VALUES ('single ''quotes'' and double "quotes"')


--
Best regards

 Sergey Startsev
 SQLite Analyzer - GUI tool to manage SQLite databases.
 http://www.kraslabs.com/sqlite_analyzer.html

Reply | Threaded
Open this post in threaded view
|

RE: How to insert a string using C interface that contains both single and double quotes

John Buck

Try this

char * EscapedQuery  = sqlite3_mprintf("%q",MyQuery);

///.. Do some Stuff .. execute query..etc..

sqlite3_free(EscapedQuery);


--
JB

-----Original Message-----
From: Sergey Startsev [mailto:[hidden email]]
Sent: Wednesday, May 25, 2005 11:45 AM
To: Sankara Narayanan
Subject: Re: [sqlite] How to insert a string using C interface that contains
both single and double quotes

Hello,

Wednesday, May 25, 2005, 11:30:00 PM, you wrote:

SN> single and double quotes. I am using the C APIs of sqlite3_prepare and
SN> sqlite3_exec to insert data into the database. How do I prepare the
query
SN> string in such case where the string data have both single and double
SN> quotes.

This insert works currently:

INSERT INTO test (str)
VALUES ('single ''quotes'' and double "quotes"')


--
Best regards

 Sergey Startsev
 SQLite Analyzer - GUI tool to manage SQLite databases.
 http://www.kraslabs.com/sqlite_analyzer.html


Reply | Threaded
Open this post in threaded view
|

RE: How to insert a string using C interface that contains both single and double quotes

John Buck
In reply to this post by Sankara Narayanan

BTW..
"MyQuery" is not your full Insert or Select Statement.
I guess I should have called it MyString...
It is just your "String" you need escaped so you can put it into a query.

A better example would be:

char * value = "'quotes'";

char * SafeString= sqlite3_mprintf("%q",value);

...


-----Original Message-----
From: John Buck [mailto:[hidden email]]
Sent: Wednesday, May 25, 2005 12:09 PM
To: '[hidden email]'
Subject: RE: [sqlite] How to insert a string using C interface that contains
both single and double quotes


Try this

char * EscapedQuery  = sqlite3_mprintf("%q",MyQuery);

///.. Do some Stuff .. execute query..etc..

sqlite3_free(EscapedQuery);


--
JB

-----Original Message-----
From: Sergey Startsev [mailto:[hidden email]]
Sent: Wednesday, May 25, 2005 11:45 AM
To: Sankara Narayanan
Subject: Re: [sqlite] How to insert a string using C interface that contains
both single and double quotes

Hello,

Wednesday, May 25, 2005, 11:30:00 PM, you wrote:

SN> single and double quotes. I am using the C APIs of sqlite3_prepare and
SN> sqlite3_exec to insert data into the database. How do I prepare the
query
SN> string in such case where the string data have both single and double
SN> quotes.

This insert works currently:

INSERT INTO test (str)
VALUES ('single ''quotes'' and double "quotes"')


--
Best regards

 Sergey Startsev
 SQLite Analyzer - GUI tool to manage SQLite databases.
 http://www.kraslabs.com/sqlite_analyzer.html


Reply | Threaded
Open this post in threaded view
|

Re: How to insert a string using C interface that contains both single and double quotes

Jay Sprenkle
In reply to this post by Sankara Narayanan
You could use the api call that binds variables to parameters in the sql.
The sql parser doesn't read the data when you do, so it won't matter
what's in it:

    sqlite3_bind_text( pStmt, Index, pointer_to_column_data,
column.size(), SQLITE_STATIC );

sql:
insert into mytable (x) values(?);



On 5/25/05, Sankara Narayanan <[hidden email]> wrote:

> Hi,
>
> I have a tricky situation where I have strings in my data that have both
> single and double quotes. I am using the C APIs of sqlite3_prepare and
> sqlite3_exec to insert data into the database. How do I prepare the query
> string in such case where the string data have both single and double
> quotes.
>
> FYI, The string data are some meta data information and hence they can
> contain all possible characters with any possible combination and there is
> not restriction for special characters.
>
> Your suggestions are highly appreciated.
>
> Thank you,
>
> With regards,
> Sankara Narayanan B.
>


--
---
You a Gamer? If you're near Kansas City:
Conquest 36
https://events.reddawn.net

The Castles of Dereth Calendar: a tour of the art and architecture of
Asheron's Call
http://www.lulu.com/content/77264