0

Is there a method in which I can retrieve all the data from a particular database column and store it in a string array? My database currently looks like this:

public class DatabaseTotalEntries {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "appliance_name";
public static final String KEY_TOTAL_TIME = "appliance_running_time";
public static final String KEY_TOTAL_COST = "appliance_cost_usage";
public static final String KEY_TOTAL_ENERGY = "appliance_energy_usage";

private static final String DATABASE_NAME = "ApplianceTotaldb"; // The name of the database

private static final String DATABASE_TABLE = "AllEntries"; // The name of the database table

private static final int DATABASE_VERSION = 1;

and I am looking to create a method in which the data under the column 'KEY_TOTAL_COST' for every row/entry in the database is retrieved and stored in a String array

Any hep would be appreciated

Sketzii
  • 181
  • 1
  • 2
  • 10

2 Answers2

2

Use standard way to retrieve data from DB via JDBC driver for your database. Create prepared statement, execute it upon created connection and pull data from result set object. As mentioned in Mihai Todor's answer, in SQL query, select only KEY_TOTAL_COST, it will be faster if you query only for columns that you're interested in. When you pull data from result set, you can put it into whatever data structure you want, and manipulate it as you need (truncate, split, convert to numbers and so on ...)

You might find this tutorial useful: http://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html ... or simply try looking on google for more of "JDBC result set" usage examples.

Martinecko
  • 1,719
  • 4
  • 22
  • 35
1

Well, the SQL statement that you need to use is this: select appliance_running_time from ApplianceTotaldb.AllEntries. In order to run it, I guess it depends on the RDBMS that you are using. Is it MySQL? Try to search for a JDBC tutorial on Google...

Mihai Todor
  • 8,014
  • 9
  • 49
  • 86
  • In this case, you should follow the instructions from http://stackoverflow.com/questions/8820859/java-sqlite-for-dummies and execute the SQL statement that I mentioned before. – Mihai Todor Apr 29 '12 at 21:10