We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Best practice - MySQL, Arrays and ArrayLists
Page Index Toggle Pages: 1
Best practice - MySQL, Arrays and ArrayLists (Read 160 times)
Best practice - MySQL, Arrays and ArrayLists
Jan 12th, 2009, 1:38pm
 
Hello, this is my first post, I'm pretty new to Processing and I'm really enjoying using it so far!

In the interest of bettering my programming skills, I'm posting some code that does work, but I'm looking for suggestions on how to improve it. And because this code may be of use to someone.

It involves querying a MySQL database, building on Jake's post in this thread: http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1133759630

The code below pulls categories from a MySQL database, along with the number of times each category appears. It does this using some PHP (also below). I'm working on code to visualize the resulting data. For now, here's the code from two PHP files and then the Processing code to collect database data. Thanks in advance for any suggestions about how to simplify or improve the code, or to adapt it so it fits best practice!

From read_categories.php:
Code:

<?PHP
$dblink = mysql_connect("localhost:8889", "login", "pass");
mysql_select_db("db_for_processing");

$sql = "
SELECT categories.c_id, categories.category_name
FROM categories
ORDER BY categories.c_id
";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {

echo $row['category_name'];

echo "\t";

echo $row['c_id'];
echo "\n";
}
?>


From check.php:
Code:

<?PHP
$dblink = mysql_connect("localhost:8889", "login", "pass");
mysql_select_db("db_for_processing");

$which_id = $_GET['which_id'];

$sql = "
SELECT c_id, COUNT( * ) as theCount
FROM category_relationship
WHERE c_id = '$which_id'
GROUP BY c_id
";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {

echo $row['c_id'];

echo "\t";

echo $row['theCount'];
echo "\n";
}

?>


And the Processing code (with comments):
Code:

String[] cat_data_from_SQL;
String[] catIDsStrings;
String[] categoryNames;
String[] categoryIDsAppStr;
String[] number_each_catStr;
int[] categoryIDs;
int[] categoryIDsApp;
int[] number_each_cat;
ArrayList list_of_counted_cats;
int how_many_categories;
int cats_with_appearances;

void setup() {
size(100, 100);

/* read list of categories from DB, which contains names and IDs. Then go through the array with this data from the SQL query and split it into two more arrays, one for the category names and another for the category IDs. Would it be simpler here to create a single Array of a Category class?
*/

cat_data_from_SQL = sqlQuery("http://localhost:8888/db_link_test/read_categories.php");
how_many_categories = cat_data_from_SQL.length;
catIDsStrings = new String[how_many_categories];
categoryNames = new String[how_many_categories];

for (int i=0; i<how_many_categories; i++) {
String[] pieces = split(cat_data_from_SQL[i], TAB);
categoryNames[i] = pieces[0];
catIDsStrings[i] = pieces[1];
}
/* Finally I need the category IDs as ints. */
categoryIDs = int(catIDsStrings);

/* Query again to find out how many appearances each category ID makes - as in, find how many times each category appears, do this by going through each category (or 'cat' for short) ID and querying the DB through a for loop, then if there is data (meaning that the SQL query returned something because there were category appearances to COUNT (see check2.php) then store the query into an ArrayList.

I'm using two arrays, categoryIDsAppStr and number_each_catStr, to hold the results from the splitting of the loadStrings results, then converting them separately into another array as type int. Is there a simpler way without using so many arrays? */

list_of_counted_cats = new ArrayList();
for (int n=0; n<=how_many_categories; n++) {
String[] temp = sqlQuery("http://localhost:8888/db_link_test/check.php?which_id=" + n);
if(temp.length != 0) {
list_of_counted_cats.add(temp[0]);
println("Category data present for cat ID " + n + ":");
println("Category list so far: "+list_of_counted_cats);
}
}

cats_with_appearances = list_of_counted_cats.size();
categoryIDsAppStr = new String[cats_with_appearances];
number_each_catStr = new String[cats_with_appearances];

for (int u=0; u<cats_with_appearances; u++) {
String to_get_cut = (String)list_of_counted_cats.get(u);
String[] cut = split(to_get_cut, TAB);
categoryIDsAppStr[u] = cut[0];
number_each_catStr[u] = cut[1];
println("Category ID "+categoryIDsAppStr[u]+" appears "+ number_each_catStr[u]+" times.");
}
categoryIDsApp = int(categoryIDsAppStr);
number_each_cat = int(number_each_catStr);

}

void draw() {

}

String[] sqlQuery(String query) {
String[] temp = loadStrings(query);
return temp;
}

Page Index Toggle Pages: 1