Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a DataDistribution

data = KernelMixtureDistribution[RandomVariate[NormalDistribution[], 10^5]] 

that takes a long time to calculate because of database operations. I don't need to constantly recalculate the distribution, so I'd like to store it in a database, and recalculate it at intervals.

The documentation for SQLBinary refers to ExportString, which seems focused on graphics and doesn't have a generic format. Nor does StringTake work on a DataDistribution.

So, how can I store a DataDistribution in an SQL database?


Thank you again Andy Ross!

Here's my solution in the end. creating a string version of a data distribution.

inform = kern // InputForm;

Then I save it into an SQL database with SQLInsert.

Next, to extract it back to usable form:

ToExpression[ ToString[sqlkern] , InputForm]
share|improve this question
up vote 5 down vote accepted

The internals of the DataDistribution are necessary for reconstructing it. I don't know much about database connectivity but Export does the right thing.

Export["temp.txt", KernelMixtureDistribution[Range[10]]];

Since I chose a .txt file Import comes back with a string.

Import["temp.txt"] // Head


In order to compute with it needs to be an expression.

dist = ToExpression[Import["temp.txt"]];


(* 11/2 *)

I chose the .txt file to show this potential issue since you may need to do something similar when bringing it back from a database.

share|improve this answer
I guess I should have asked this also, how do I reconstruct the input form? Just read it back into a variable? – Steven Dec 6 '12 at 3:44
Hrm. //InputForm seems to do the right thing in making a nice text format version of the Distribution. I can save that easily to a database. But I still can't figure out how to reconstruct a working distribution from that. – Steven Dec 6 '12 at 4:01
I got it. You need to do ToString within the ToExpression function. inform = KernelMixtureDistribution[Flatten[sampleData], MaxMixtureKernels -> All]; // InputForm; ToExpression[ ToString[inform] , InputForm] Works. – Steven Dec 6 '12 at 4:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.