List:MySQL++« Previous MessageNext Message »
From:Warren Young Date:May 11 2009 9:49pm
Subject:Re: Insert/Get array of ints to/from table
View as plain text  
On May 11, 2009, at 7:05 AM, Bruno Adami wrote:

> How would I insert/get an array of integers to/from the database?  
> What data type should I use to do it in my table?

While there are ways to store an array in a relational database as a  
single unit, it's almost always the wrong thing to do.

What you should use instead is a separate table, keyed to the first  
table.  Example:

	Table foo
	---------
	id
	column1
	column2

	Table bar
	---------
	foo_id
	some_int

Then, when you have a foo table row, you can get the associated bar  
array with something like this:

	query << "SELECT some_int FROM bar WHERE foo_id = " <<
		foo_instance["id"];

This really is elementary.  It's called normalization, and is covered  
in any decent book on database design, along with the reasons why this  
structure makes more sense in a relational database than the way you'd  
do it in C++.  Please read up on the topic before opening more threads  
on database design.  This wheel is already invented.
Thread
Insert/Get array of ints to/from tableBruno Adami11 May
  • Re: Insert/Get array of ints to/from tableWarren Young11 May