Int from Database getting cut short

Put your problem here if it does not fit any of the other categories.

Int from Database getting cut short

Postby ramgraph1 » Wed Feb 20, 2008 12:02 am

I am storing an integer in a database and calling it into another class. When I retrieve it, the integer is getting truncated - so 110 becomes 1, 230 becomes 2, 470 becomes 4, etc.
I tried both "long" and "int" in the code and both have the same results. What am I doing wrong?

Here is the relevant database helper class code:

Code: Select all
public long createNote(String username, String comment, int playerscore) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_username, username);
        initialValues.put(KEY_comment, comment);
        initialValues.put(KEY_score, playerscore);
        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

public Cursor fetchNote(long rowId) throws SQLException {
        Cursor result = mDb.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_username, KEY_comment, KEY_score}, KEY_ROWID + "=" + rowId, null, null,
                null, null);
        if ((result.count() == 0) || !result.first()) {
            throw new SQLException("No note matching ID: " + rowId);
        }
        return result;
    }


And here is the relevant code from the class that is using the database to store and retrieve info:


Code: Select all
   
        dbhelper.createNote(userName, userComment, score);


Code: Select all
       Cursor c = dbhelper.fetchNote(userNumber);
       thisusersname = c.getString(c.getColumnIndex(dbhelper.KEY_username));
       thisuserscomment = c.getString(c.getColumnIndex(dbhelper.KEY_comment));
       thisuserscore = c.getInt(c.getColumnIndex(dbhelper.KEY_score));
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Top

Postby cadlg » Wed Feb 20, 2008 12:45 am

Hi ramgraph1.
I have not checked the documentation about the database, but in a previous post I noticed that you are creating your table using a 'text' type for all fields, but when fetching data, you are trying to fetch an 'int' or 'long' type.

It is not possible to create the 'score' field in the database as INTEGER or any other numeric type ?

Regards,
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby ramgraph1 » Wed Feb 20, 2008 3:34 am

OK. I understand better what that create table code is saying now. I should have caught that!
Thanks for answering that for me!
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests