Re: [Mysql-cocoa-users] Inserting and getting blobs
Brought to you by:
sergecohen
|
From: John G. <jo...@jl...> - 2002-07-23 16:10:50
|
I've been using a lot of BLOBs in my current test, mainly because=20
LONGTEXT are not supported yet and I have been inserting large text=20
files and templates into my database.
So what I have been doing is convert my NSString to NSData using the=20
method of NSString, and then preparing my NSData back into a string to=20=
insert in SQL.
Actually I have a helper object that does the conversion and has the=20
following method:
-(NSString*)prepareString:(NSString*)sourceString
{
// convenience method, it converts a string into an NSData
// object then converts it back to an SQL safe string
NSData* binData =3D [sourceString=20
dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
=09
return([dbConnection prepareBinaryData:binData]);
}
and here is how it would be used in SQL
NSString* sql =3D [NSString stringWithFormat:
@"INSERT INTO templates (ID, NAME, TEMPLATESET, TEMPLATE)=20
VALUES('%@', '%@', %@, '%@')",
nextId, newName, newSet, [dbConnection=20
prepareString:templateContents]];
if (res =3D [dbConnection runQuery:sql])
{
// All OK
}
As Serge says, when you pull the blob back out of a query it arrives as=20=
an NSData object so you just need to convert it back to your own=20
preferred type.
I hope this helps people.
Serge, in future releases you may want to add a function like my=20
prepareString method since I use it all the time when inserting user=20
provided data into the DB since they may provide characters that are not=20=
SQL safe.
JOhn
On Tuesday, July 23, 2002, at 04:07 PM, Serge Cohen wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hello again;
>
> About blob the best is to put them in a NSData, use the method=20
> prepareBinaryData: from MCPConnection to transform it into a string=20
> (you still should make the single quotes yourself) that you can insert=20=
> into a query (obviously insert or update...).
>
> To get it back, it just should work ok. In a row, a blob is put in a=20=
> NSData; so you should be able to use directly as input to one o the=20
> NSImage init method.
>
> Indeed I'm very interested on the result, I've done only a very small=20=
> test on both inserting blob using NSData and prepareBinaryData:, and=20=
> getting a NSData from a DB blob.
>
> Serge.
>
>
> Le jeudi 18 juillet 2002, =E0 12:13 , Jerome Rabas a =E9crit :
>
>> Does anybody know how to insert/get blobs from the DB?
>>
>> My problem is that I have a NSImage and I want to insert its content=20=
>> in the DB and, then, be able to get it.
>> What conversion should I do? (converting to NSData, and then?)
>>
>>
>> Thanks.
>>
>>
>> Jerome.
>>
>>
>>
>> -------------------------------------------------------
>> This sf.net email is sponsored by:ThinkGeek
>> Welcome to geek heaven.
>> http://thinkgeek.com/sf
>> _______________________________________________
>> Mysql-cocoa-users mailing list
>> Mys...@li...
>> https://lists.sourceforge.net/lists/listinfo/mysql-cocoa-users
>>
>>
> - ----------------------------------------------------
> Serge Cohen
>
> GPG Key ID: 1024D/69B1D346
> - ----------------------------------------------------
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.0.6 (Darwin)
> Comment: For info see http://www.gnupg.org
>
> iD8DBQE9PXEmMygj1Wmx00YRAkXbAJ4ji6AwVd8WMrge5niUU9KPhdqJrgCeJ/6F
> tdinAH4SvLGJDYeCIzbxwRk=3D
> =3DrokP
> -----END PGP SIGNATURE-----
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Mysql-cocoa-users mailing list
> Mys...@li...
> https://lists.sourceforge.net/lists/listinfo/mysql-cocoa-users
>
|