views:

68

answers:

2

I'm using a SQL database to store a single float value, and I'm really having trouble getting my brain around everything that needs to be done to make it work. I've been reading the NotePad tutorial Google provides for a few days now and Googling around but it just isn't clicking in my head.

Could anyone explain to me (no code needed) just what exactly I need to have for a simple database, and how to read the value from it into my float variable and how to write the value of the variable back to the table?

Much thanks, I think my brain is starting to seep out my ears.

+1  A: 

A SQL database is not a very good way to store a single float value. It's overkill. Instead, I recommend just using Android's SharedPreference class, which provides a simple key-value store.

If you're still going to create a database, what you need is:

  1. The database file.
  2. A SQL schemea for that database file. This describes the tables in the database, as well as the columns. If you're just storing a single float, then you could just create a single table ("data"), with two columns ("key", "value") -- like SharedPreferences, we're just implementing a key-value store of our own (another reason not to do this -- you're reinventing the wheel).

Once that's created, you can just insert a record with some arbitrary key ("myFloat") for lookups later and your chosen value.

So, your initial SQL statement would look something like this:

INSERT INTO data (key, value) VALUES ("myFloat", 3.14);

Later, you'd retrieve it with a SELECT statement:

SELECT key, value FROM data WHERE key="myFloat";

And you can update the value with an UPDATE statement:

UPDATE data SET value=3.14 WHERE key="myFloat";
Trevor Johns
Haha yea I know using a SQLitedatabase would be overkill, but I wanted to learn how to use it as databases are so versatile. I'm ok with the SQL commands, but what's getting me is how I could read the value from the table into a variable using android. It's the android layer that is confusing me o_0
jamzsabb
Got it figured out, thanks in large part to your post, thanks a lot!
jamzsabb
A: 

It is as simple as shown above or just try reading more on SQL queries

Mike