Re: [Mysql-cocoa-users] Inserting and getting blobs
Brought to you by:
sergecohen
|
From: Serge C. <co...@em...> - 2002-07-24 10:04:48
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
=0D
Indeed in the version-2 framework there is a prepareString: method, =0D
there is also a method to convert a NSData to a NSString using the =0D
encoding of the DB (stringWithText:); it's in both MCPConnection and =0D
MCPResult (as both these class hold an encoding informaion).=0D
=0D
The conversion method (prepareString:) prepare a string to be inserted =0D=
in a query, hence it will escape single and double quotes plus other =0D
things (based on the C API function mysql_real_escape_string).=0D
=0D
Indeed the prepareString already existed in version-1, but it was not =0D=
implemented in the same way (don't remember precise difference). the =0D
stringWithText: exist only in v-2.=0D
=0D
Serge.=0D
=0D
PS: the problem for the LONGTEXT, is that it's not recognised by the C =0D=
API, which does not make difference between TEXT and BLOBS. So far I =0D
don't have any thing to get the info if a column is TEXT or BLOB, so I =0D=
treat both as BLOB.=0D
If anyone experimented with C API, knows how to make the diff, tell me =0D=
I'll use it.=0D
=0D
=0D
Le mardi 23 juillet 2002, =E0 06:11 , John Guy a =E9crit :=0D
=0D
> I've been using a lot of BLOBs in my current test, mainly because =0D
> LONGTEXT are not supported yet and I have been inserting large text =0D=
> files and templates into my database.=0D
>=0D
> So what I have been doing is convert my NSString to NSData using the =0D=
> method of NSString, and then preparing my NSData back into a string to =
=0D
> insert in SQL.=0D
>=0D
> Actually I have a helper object that does the conversion and has the =0D=
> following method:=0D
>=0D
> -(NSString*)prepareString:(NSString*)sourceString=0D
> {=0D
> // convenience method, it converts a string into an NSData=0D
> // object then converts it back to an SQL safe string=0D
> NSData* binData =3D [sourceString =0D
> dataUsingEncoding:NSASCIIStringEncoding=0D
> allowLossyConversion:YES];=0D
> =0D
> return([dbConnection prepareBinaryData:binData]);=0D
> }=0D
>=0D
> and here is how it would be used in SQL=0D
>=0D
> NSString* sql =3D [NSString stringWithFormat:=0D
> @"INSERT INTO templates (ID, NAME, TEMPLATESET, TEMPLATE) =0D
> VALUES('%@', '%@', %@, '%@')",=0D
> nextId, newName, newSet, [dbConnection =0D
> prepareString:templateContents]];=0D
>=0D
> if (res =3D [dbConnection runQuery:sql])=0D
> {=0D
> // All OK=0D
> }=0D
>=0D
> As Serge says, when you pull the blob back out of a query it arrives =
as =0D
> an NSData object so you just need to convert it back to your own =0D
> preferred type.=0D
>=0D
>=0D
> I hope this helps people.=0D
>=0D
> Serge, in future releases you may want to add a function like my =0D
> prepareString method since I use it all the time when inserting user =0D=
> provided data into the DB since they may provide characters that are =0D=
> not SQL safe.=0D
>=0D
> JOhn=0D
>=0D
> On Tuesday, July 23, 2002, at 04:07 PM, Serge Cohen wrote:=0D
>=0D
>> -----BEGIN PGP SIGNED MESSAGE-----=0D
>> Hash: SHA1=0D
>>=0D
>> Hello again;=0D
>>=0D
>> About blob the best is to put them in a NSData, use the method =0D
>> prepareBinaryData: from MCPConnection to transform it into a string =0D=
>> (you still should make the single quotes yourself) that you can =
insert =0D
>> into a query (obviously insert or update...).=0D
>>=0D
>> To get it back, it just should work ok. In a row, a blob is put in a =0D=
>> NSData; so you should be able to use directly as input to one o the =0D=
>> NSImage init method.=0D
>>=0D
>> Indeed I'm very interested on the result, I've done only a very small =
=0D
>> test on both inserting blob using NSData and prepareBinaryData:, and =0D=
>> getting a NSData from a DB blob.=0D
>>=0D
>> Serge.=0D
>>=0D
>>=0D
>> Le jeudi 18 juillet 2002, =E0 12:13 , Jerome Rabas a =E9crit :=0D
>>=0D
>>> Does anybody know how to insert/get blobs from the DB?=0D
>>>=0D
>>> My problem is that I have a NSImage and I want to insert its content =
=0D
>>> in the DB and, then, be able to get it.=0D
>>> What conversion should I do? (converting to NSData, and then?)=0D
>>>=0D
>>>=0D
>>> Thanks.=0D
>>>=0D
>>>=0D
>>> Jerome.=0D
>>>=0D
>>>=0D
>>>=0D
>>> -------------------------------------------------------=0D
>>> This sf.net email is sponsored by:ThinkGeek=0D
>>> Welcome to geek heaven.=0D
>>> http://thinkgeek.com/sf=0D
>>> _______________________________________________=0D
>>> Mysql-cocoa-users mailing list=0D
>>> Mys...@li...=0D
>>> https://lists.sourceforge.net/lists/listinfo/mysql-cocoa-users=0D
>>>=0D
>>>=0D
>> - ----------------------------------------------------=0D
>> Serge Cohen=0D
>>=0D
>> GPG Key ID: 1024D/69B1D346=0D
>> - ----------------------------------------------------=0D
>> -----BEGIN PGP SIGNATURE-----=0D
>> Version: GnuPG v1.0.6 (Darwin)=0D
>> Comment: For info see http://www.gnupg.org=0D
>>=0D
>> iD8DBQE9PXEmMygj1Wmx00YRAkXbAJ4ji6AwVd8WMrge5niUU9KPhdqJrgCeJ/6F=0D
>> tdinAH4SvLGJDYeCIzbxwRk=3D=0D
>> =3DrokP=0D
>> -----END PGP SIGNATURE-----=0D
>>=0D
>>=0D
>>=0D
>> -------------------------------------------------------=0D
>> This sf.net email is sponsored by:ThinkGeek=0D
>> Welcome to geek heaven.=0D
>> http://thinkgeek.com/sf=0D
>> _______________________________________________=0D
>> Mysql-cocoa-users mailing list=0D
>> Mys...@li...=0D
>> https://lists.sourceforge.net/lists/listinfo/mysql-cocoa-users=0D
>>=0D
>=0D
>=0D
>=0D
> -------------------------------------------------------=0D
> This sf.net email is sponsored by:ThinkGeek=0D
> Welcome to geek heaven.=0D
> http://thinkgeek.com/sf=0D
> _______________________________________________=0D
> Mysql-cocoa-users mailing list=0D
> Mys...@li...=0D
> https://lists.sourceforge.net/lists/listinfo/mysql-cocoa-users=0D
>=0D
>=0D
- ----------------------------------------------------=0D
Serge Cohen=0D
=0D
GPG Key ID: 1024D/69B1D346=0D
- ----------------------------------------------------=0D
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (Darwin)
Comment: For info see http://www.gnupg.org
iD8DBQE9PnvPMygj1Wmx00YRAhMCAJ0Qzrvcg6gZEmsfawo4eKHcFMG46ACeLnfE
85yie9vLmt9g4qbh69fawMc=3D
=3DFAch
-----END PGP SIGNATURE-----
|