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 › MySQL String Array
Page Index Toggle Pages: 1
MySQL String Array (Read 760 times)
MySQL String Array
Mar 14th, 2007, 3:54pm
 
Hello all,

I've created a class using the MySQL library. My goal is to query a table from my database and output the result as a list of strings. So far I can get it to output the list of database  entries in my class method getData(); but when I try to return this list of data I only get the last entry in the db. How do I fix this - create a String Array?

Quote:


class sql {  
   String user = "-user-";
   String pass = "-pass-";
   String database = "-db-";
   String table = "-table-";
   String host = "localhost";
   
   String question;
   
   // Constructor
   sql(PApplet _p)
   {
     msql = new MySQL( host, database, user, pass, _p);
 
   if ( !msql.connect() )
   
   println("connection failed");
   
 }  
   
   String getData() {
   msql.query( "SELECT * FROM " + table );
   while (msql.next())
       {
           String q = msql.getString("question");
           question = q;
           println(q);       // Ouputs list of strings here OK!, But...
     }
     return question; // Only last string value is output here.... *** OUTPUT "String Array" here ? ***      
   }
 
}





Much appreciated!

Brendan
Re: MySQL String Array
Reply #1 - Mar 14th, 2007, 4:16pm
 
You're only getting a single string, since that's what you're returning. If you want to return an array, you need to put the data into an array...

You only need to change 3 lines:

Code:

// String question;
String[] question;

...
// String getData() {
String[] getData(){

...

// question = q;
question = append(question,q);

Re: MySQL String Array
Reply #2 - Mar 14th, 2007, 4:57pm
 
Thanks for the reply John. I now get a NullPointerException error at "question = append(question,q);". Here's the new code:

Quote:


class sql {  
   String user = "-user-";
   String pass = "-pass-";
   String database = "-db-";
   String table = "-table-";
   String host = "localhost";
   String[] question;
   
   // CONTRUCTOR
   sql(PApplet _p)
   {
     msql = new MySQL( host, database, user, pass, _p);
 
   if ( !msql.connect() )
   
   println("connection failed");
   
 }  
   
   String[] getData() {
   
   msql.query( "SELECT * FROM " + table );

   while (msql.next())
       {
           String q = msql.getString("question");
           question = append(question,q);
           println(q);      
     }
     return question;      
   }  
}


Re: MySQL String Array
Reply #3 - Mar 14th, 2007, 5:27pm
 
Ah yes, sorry, you need to add "question=new String[0];" into your constructor I think.
Page Index Toggle Pages: 1