About Store Forum Documentation Contact
Donations:
364$/mo



Post Reply 
[RESOLVED] Saving/Reading binary data in SQL
Author Message
aceio76 Offline
Silver Supporter

Post: #1
[RESOLVED] Saving/Reading binary data in SQL
Given the following code snippet:
Code:
SQL sql;
Str messages;
.
.
SomeObject obj;
sql.newRow(_table, SQLValues().New("binary", &obj, sizeof obj), &messages);
.
.
for(;GameSystemsDB.db.getNextRow();)
{
  SomeObject nobj; Int sz=600; sql.getCol(2, &nobj, sz);
}
I can see that it does create an entry in the _table with the binary data, but when I try to read it back as an object of the same type, it crashes the application.

What is the right way of saving (just in case I have it wrong) and reading binary data from the sql? Should I write a process that will serialize the object first and save it as an xml data instead? I'd like to be able to save objects in the db and I'm looking for the quickest and best way to do it. I've also tried saving the File f; as binary but once again, I could not read it back without crashing.

www.thinkplaystudios.com
(This post was last modified: 10-03-2011 05:05 AM by aceio76.)
09-22-2011 04:23 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #2
RE: Saving/Reading binary data in SQL
1. remember that binary column has constant memory size limit SQLColumn::binary_size (use -1 for variable/unlimited length)

2. using New("binary", &obj, sizeof obj) you're writing raw memory, which is not good, you should write save/load methods for the object, write it to File, then copy memory from File to continuous memory, and set that memory pointer into SQLValues

read using same process, to memory, then to File, then use object.load from file

edit: I will add SQL methods that operate also on File's
09-22-2011 12:53 PM
Find all posts by this user Quote this message in a reply
aceio76 Offline
Silver Supporter

Post: #3
RE: Saving/Reading binary data in SQL
I was .set("binary", SDT_BINARY , 8000); but now I know -1 sets it as variable, I used that.

I still cannot get it to load into the object, although with the new size, it no longer crashes the application after writing and attempting to read.

The closest thing I could get to what I want the end result to be is to break the object apart into its object name and its members/values, save that info as text (xml format, or something I can understand), and when reading it back from the db, I create a new object based on the name, and put values into the created object's members based on the data read from the db.

With the change you have in mind, do you mean I can save/load directly to File and then load that into an object?

www.thinkplaystudios.com
09-23-2011 02:34 AM
Visit this user's website Find all posts by this user Quote this message in a reply
aceio76 Offline
Silver Supporter

Post: #4
RE: Saving/Reading binary data in SQL
Thank you for adding the save file as binary feature today.

www.thinkplaystudios.com
09-26-2011 02:18 AM
Visit this user's website Find all posts by this user Quote this message in a reply
aceio76 Offline
Silver Supporter

Post: #5
RE: Saving/Reading binary data in SQL
Just a heads up, Sql supports reading and writing U64 through SQLValues().New() but the SQL_DATA_TYPE enum only supports SDT_LONG which is a 64bit signed integer (not BIGINT).

www.thinkplaystudios.com
09-30-2011 10:39 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #6
RE: Saving/Reading binary data in SQL
what do you mean?
there is SDT_LONG for I64 values
10-01-2011 10:36 AM
Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #7
RE: Saving/Reading binary data in SQL
He's concerned about unsigned integer types. As you said, SDT_LONG is for I64 values, but what about U64 values? Should there be a SDT_ULONG or is SDT_LONG used for both I64 and U64 values?
10-01-2011 07:53 PM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #8
RE: Saving/Reading binary data in SQL
sql doesn't support natively U64, U64 is just casted to I64
10-01-2011 07:59 PM
Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #9
RE: Saving/Reading binary data in SQL
U64 is not a SQL standard, but MySQL does indeed support it: Integer Types.

What about U8, U16, and U32? These values are also just cast to their signed integer counterpart?
10-01-2011 08:14 PM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #10
RE: Saving/Reading binary data in SQL
ms sql doesn't support unsigned

Quote:What about U8, U16, and U32? These values are also just cast to their signed integer counterpart?
yes
10-01-2011 08:28 PM
Find all posts by this user Quote this message in a reply
aceio76 Offline
Silver Supporter

Post: #11
RE: Saving/Reading binary data in SQL
Sorry I missed the replies on this. This is really just to point it out but it is not relevant to my project, but it may be for another. Yes you definitely can do unsigned 64-bit int (not through BIGINT, sorry I mis-quoted).

You can store the value in a NUMERIC type with a scale of 0, which will retain the integer semantics required. The NUMERIC type will allow negative numbers, although you could set up a constraint to require positive integers.

The maximum precision for NUMERIC is 38 decimal digits. 2**64 is somewhere around 18 or 19 decimal digits, so NUMERIC(19,0) would likely work just fine for this data.

I tend to just use NUMERIC(20) with no complications.

www.thinkplaystudios.com
(This post was last modified: 10-01-2011 09:42 PM by aceio76.)
10-01-2011 09:41 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply