Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to save array in database(mysql), My array contain 50 values e.g
a[0,1,0]= 2.3;
a[0,2,0]=8;
a[0,2,1]= 6;
.
.
.
a[0,50,1]=4.5

now I want to save this array in database, another way is to read value of each element of array and connate them to string and save in database but I do not want to do in that way. I directly want to save array in database as table tuple.

regards
Posted
Updated 1-Oct-13 22:55pm
v5
Comments
FoxRoot 2-Oct-13 4:58am    
Use cascade iteration. For under for. And insert into the database every time in the related for scope.

You can do it, just uses a VARBINARY format column in your database, and a BinaryFormatter in your C# code to convert the multidimensional array of doubles to a "flat" array of bytes:
C#
double[, ,] ar = new double[2, 3, 4];
ar[0, 1, 0] = 2.3;
ar[0, 2, 0] = 8;
ar[0, 2, 1] = 6;
...
ar[1, 2, 1] = 4.5;
// Convert to bytes
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, ar);
byte[] data = ms.GetBuffer();
// Save bytes to DB
Then when you read them back as bytes:
C#
// Convert from bytes
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
double[, ,] ar2 = (double[,,])bf.Deserialize(ms);
 
Share this answer
 
Comments
rameshKumar1717 2-Oct-13 13:05pm    
Thank You
OriginalGriff 2-Oct-13 13:49pm    
You're welcome!
I believe (but could be wrong) that 'tuple' in relational database terms means 'the group of columns that comprises a table', more or less.

On the basis of that definition, it would appear that you want to have a table with one column for each element in your array. Hopefully, I've misinterpreted you, because that is not a robust architecture ... when the array grows you'd need to add new columns (when, in a relational database, adding rows is the appropriate response, which is easy).

So, here's a solution that I think would work.

Your array is three dimensional, with each dimension being indexed by an int. The index set (tuple) that identifies a specific value in the array must be the Primary Key of the database table that holds the array values; the table will have one other column that contains the value, and may have others if you need to track when and by whom values were changed.

Here's a table definition that would accomplish this:

SQL
CREATE TABLE GiveMeAMeaningfulName
  (
  Index1 INT NOT NULL,             -- Give me a better name!
  Index2 INT NOT NULL,             -- And me!
  Index3 INT NOT NULL,             -- Me too
  Value FLOAT NOT NULL,            -- This column's name should probably relate to the table name; its type might be better as NUMERIC(xx,yy); if you were using a sparse array, it could be NULL, but you're not, so it is NOT NULL

  -- Optional change tracking
-- CreatedBy INT NOT NULL,  -- Data type as needed for your user table primary key
-- CreatedOn DATETIME NOT NULL,
-- AmendedBy INT NULL,
-- AmendedOn DATETIME NULL,

  -- Necessary primary key
  PRIMARY KEY (Index1, Index2, Index3)
  )
GO


Note - I work with Microsoft SqlServer; my SQL dialect may not work perfectly with MySql. The idea should nevertheless be valid.

With a table design like that you are free to save all elements of your array, or just those that have changed. As with all other times when a collection must be saved to / loaded from a database, unfortunately you have to iterate through the collection to save/load each element. There may be ways to do it with a single command that deals in bulk; if there are maybe others here will offer them (and I can learn, too!).
 
Share this answer
 
v2
Comments
rameshKumar1717 2-Oct-13 13:06pm    
Thank You.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900