0

Hey everyone,

I'm trying to get started with SQLite. My app keeps force closing on this activity:

public class ViewActivity extends Activity {
    
    private TextView company;
    private BillMeDB dh;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.all);
        
        company = (TextView) findViewById(R.id.Company1);
        BillMeDB db = new BillMeDB(this);
        
        db.open();
        long id;
        
        id = db.insertEntry("Power", "AEP", "item", "2010.9.15", "w2", "2010.9.24", "2011.1.1", 75.77);
        id = db.insertEntry("Cell Phone", "Verizon", "item", "2010.9.15", "w2", "2010.9.24", "2011.1.1", 185.45);
        
        Cursor c = db.getEntry(1);
        if (c.moveToFirst())
            company.setText(c.getString(1));
        
        db.close();  
        
//       Cursor c = BillMeLoad.database.fetchAllEntries();
            company.setText(this.dh.getEntry(0).toString());  

        }
    }

I can't figure out why though. It blows up(I think) when it starts creating a DB. Here's my DB file - I have no idea where I've gone wrong, but apparently something isn't right. Can someone give me a hand?

My DB File:

package com.caleb.billme;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class BillMeDB {

    public static final String KEY_BILLNAME = "billname";
    public static final String KEY_COMPANY = "company";
    public static final String KEY_ROWID = "_id";
    public static final String KEY_ITEM = "item";
    public static final String KEY_DUEDATE = "duedate";
    public static final String KEY_REMFREQ = "remfreq";
    public static final String KEY_REMDATE = "remdate";
    public static final String KEY_ENDDATE = "enddate";
    public static final String KEY_DUEAMNT = "dueamnt";
    
    private static final String TAG = "BillMeDB";
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;
    
    private static final String DATABASE_CREATE =
        "CREATE TABLE BillTable (_id integer primary key autoincrement, "
        + "billname text not null, company text not null, item text not null, " +
                "duedate text not null, remfreq text not null, " +
                "remdate text not null, enddate text not null, dueamnt double not null)";
    
    private static final String DATABASE_NAME = "Data";
    private static final String DATABASE_TABLE = "BillTable";
    private static final int DATABASE_VERSION = 2;
    
    private final Context mCtx;
    
    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS BillTable");
            onCreate(db);
        }
    }
    
    public BillMeDB(Context ctx) {
        mCtx = ctx;
    }
    
    public BillMeDB open() throws SQLException {
        db = DBHelper.getWritableDatabase();
        return this;
    }
    
    public void close() {
        DBHelper.close();
    }
    
    public long insertEntry(String billname, String company, String item, String duedate, String remfreq, String remdate, String enddate, double dueamnt ) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_BILLNAME, billname);
        initialValues.put(KEY_COMPANY, company);
        initialValues.put(KEY_ITEM, item);
        initialValues.put(KEY_DUEDATE, duedate);
        initialValues.put(KEY_REMFREQ, remfreq);
        initialValues.put(KEY_REMDATE, remdate);
        initialValues.put(KEY_ENDDATE, enddate);
        initialValues.put(KEY_DUEAMNT, dueamnt);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }
    
    public boolean deleteAllEntries() {
        return db.delete(DATABASE_TABLE, null, null) > 0;
    }
    
    public boolean deleteEntry(long rowId) {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }
    
    public Cursor getAllEntries() {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_BILLNAME,
                KEY_COMPANY, KEY_ITEM, 
                KEY_DUEDATE, KEY_REMFREQ, 
                KEY_REMDATE, KEY_ENDDATE, 
                KEY_DUEAMNT}, 
                null, null, null, null, null);
    }
    
    public Cursor getEntry(long rowId) throws SQLException{
        Cursor c = db.query(DATABASE_TABLE, new String[] { 
                KEY_ROWID,
                KEY_BILLNAME }, KEY_ROWID + "=" + rowId, 
                null, null, null, null);
        
        if (c != null)
            c.moveToFirst();
        
        return c;
    }
    
    
    public boolean updateEntry(long rowId, String billname, String company, String item,
            String duedate, String remfreq, String remdate, String enddate, double dueamnt) {
        ContentValues args = new ContentValues();
        args.put(KEY_BILLNAME, billname);
        args.put(KEY_COMPANY, company);
        args.put(KEY_ITEM, item);
        args.put(KEY_DUEDATE, duedate);
        args.put(KEY_REMFREQ, remfreq);
        args.put(KEY_REMDATE, remdate);
        args.put(KEY_ENDDATE, enddate);
        args.put(KEY_DUEAMNT, dueamnt);
        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}

Edited by Duki: n/a

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by Killer_Typo
0

Two things come to mind. One is you are doing a lot in onCreate. This runs before the UI is created. Maybe move the database code into a thread, ( and bear in mind that thread will have to create a handler to update the ui like that textview), that way your onCreate finishes and the program starts. Then I'd say maybe put some debug code into the db code, trys catches etc. Possibly printing to a file or even writing debug code to the textview.

Mike

0

really need to know what the line is and what error you are seeing.

Let it blow up and then post the logs here.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.