开发者

Android SQL - Notepad Tutorial

I've been looking at the notepad tutorial from the android developer site, I'm sorta stuck with two areas - can anyone help?

Firstly the tutorial creates a db with 3 columns: id, title and body. I would like to change the body to a decimal value. Secondly I need to total this column with the decimal value.

So where I have the EditText for the body column is it correct to set the input type to "numberDecimal" in the layout xml? Are any other changes necessary?

How do I then total these the values in this column, should I use be using some sort of loop to go through each record in that开发者_StackOverflow column and add them together?

Such as:

int total = 0;
for(int i = 0; i < mydb.table.NumberOfRows; i++) { 
    total = total + mydb.table.body[i]
}

* apologies I don't even know if a function to count the number of rows exists or how to access a particular part of a table (i suck at SQL and haven't been around java for a while)

Advice would be great!


Just changing the input mode of the EditText view to "numberDecimal" is not enough, but it's a good start, so you can be sure, that you user cannot input any non decimal characters.

To change the data type of the body column, you got to adapt the create statement that is used to create the table. Change the data type from TEXT to REAL so the body column can store float values.

Furthermore you got to adapt the insert statement as the body column is not a TEXT column any longer. So you got to convert the string from the EditText view which represents you numeric value to float before you pass it to the insert statement.

To sum up the all body values, you don't need a loop, you can let the db do this for you using the sum() or total() aggregation function (depending on you needs). The statement will look something like this.

SELECT sum/total(body) FROM <table name>

Replace <table name> with the name of the table.

apologies I don't even know if a function to count the number of rows exists or how to access a particular part of a table (i suck at SQL and haven't been around java for a while)

To loop over the results of a database query you don't need to know how many rows the result has. As a result of a query you get a Cursor object which references the rows returned by the query. To loop over these rows you can use the following statement:

Cursor cursor = queryDB(.......)

while(cursor.moveToNext()){
    <do something with the current result row>
}


The fact that you are using the Notepad example as the basis of what your doing, I am correct in assuming that you're using a ContentProvider? Ok so what you have to do is run a query that retrieves ALL the entries in the database:

Cursor c = managedQuery(Notes.CONTENT_URI, new String[] { Notes.BODY }, null, null, null);

Then as you were saying, loop through the cursor:

if (c.moveToFirst()) {

    long total = 0;
    long body; 
    int bodyColumn = c.getColumnIndex(Notes.BODY); 

    do {
        // Get the field values
        body = c.getLong(bodyColumn);
        phoneNumber = cur.getString(phoneColumn);
        total += body;

    } while (cur.moveToNext());

Does that answer your question? Comment if you need anything specified.


I am working on an answer to a similar question at the moment. I currently thinking that the life of the database would cause a looped tallying method to become very annoying so if I were you I would design the database to keep the tally for you and keep the total input rows. It's just a suggestion but I think it is easier to do two very simple one line methods than one continuously growing recursive/looped method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜