0

hi to all i have a small problem i am new to android and i was trying to implement and sqlite example i found on this site...

my problem is that i add a 2 columns extra to the example i found the app crashes can any one please tell me what is my mistake

this is the code:

DatabaseActivity.java

    package net.learn2develop.Database;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;

public class DatabaseActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DBAdapter db = new DBAdapter(this);

        //---add 2 titles---
        db.open();        
        long id;
        id = db.insertTitle(
                "0470285818",
                "Hanudi is the best :)",
                "Wrox",
                "www.link1.com",
                "5mi");        
        id = db.insertTitle(
                "047017661X",
                "Professional Windows Vista Gadgets Programming",
                "Wrox",
                "www.link2.com",
                "7mi");
        db.close();


        //---get all titles---
        db.open();
        Cursor c = db.getAllTitles();
        if (c.moveToFirst())
        {
            do {          
                DisplayTitle(c);
            } while (c.moveToNext());
        }
        db.close();
    }    
    public void DisplayTitle(Cursor c)
    {
        Toast.makeText(this, 
                "id: " + c.getString(0) + "\n" +
                "ADNO: " + c.getString(1) + "\n" +
                "FROM: " + c.getString(2) + "\n" +
                "TO: " + c.getString(3) + "\n" +
                "LINK: " + c.getString(4) + "\n" +
                "DIST:  " + c.getString(5),
                Toast.LENGTH_LONG).show();  
    } 

}

and this is the DBAdapter.java

package net.learn2develop.Database;

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 DBAdapter 
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NUMBER = "number";
    public static final String KEY_FROM = "fromtime";
    public static final String KEY_TO = "totime";  
    public static final String KEY_LINK = "link";
    public static final String KEY_DIST = "dist";
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "books";
    private static final String DATABASE_TABLE = "titles";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table titles (_id integer primary key autoincrement, "
        + "number text not null, from text not null, to not null " 
        + "link text not null, dist text not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    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 titles");
            onCreate(db);
        }
    }    

    //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

    //---insert a title into the database---
    public long insertTitle(String number, String from, String to, String link, String dist) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NUMBER, number);
        initialValues.put(KEY_FROM, from);
        initialValues.put(KEY_TO, to);
        initialValues.put(KEY_LINK, link);
        initialValues.put(KEY_DIST, dist);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular title---
    public boolean deleteTitle(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + 
                "=" + rowId, null) > 0;
    }

    //---retrieves all the titles---
    public Cursor getAllTitles() 
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_NUMBER,
                KEY_FROM,
                KEY_TO,
                KEY_LINK,
                KEY_DIST}, 
                null, 
                null, 
                null,
                null,
                null, 
                null);
    }

    //---retrieves a particular title---
    public Cursor getTitle(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID,
                        KEY_NUMBER, 
                        KEY_FROM,
                        KEY_TO,
                        KEY_LINK,
                        KEY_DIST
                        }, 
                        KEY_ROWID + "=" + rowId, 
                        null,
                        null, 
                        null,
                        null,
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a title---
    public boolean updateTitle(long rowId, String number, 
    String from, String to, String link, String dist) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_NUMBER, number);
        args.put(KEY_FROM, from);
        args.put(KEY_TO, to);
        args.put(KEY_LINK, link);
        args.put(KEY_DIST, dist);
        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }
}
6
  • you should only be opening and closing the db when ever you no longer need it. You should never have to close to twice back to back. It's either open or closed. Open and close it onResume and onPause. It also looks like you changed the database from the example. Be sure you have completely uninstalled the app instead of just updating it to be sure the database is recreated. Commented Mar 19, 2011 at 16:58
  • the 2 time close is a typing mistake and every time i uninstall the app and reinstall it with the new changes and for the changing part i used the code with the changes and it worked as it is it stooped working when i added the KEY_LINK and KEY_DIST and made the changes to accommodate those two new columns so i just want to know where is the problem in adding two more columns Commented Mar 19, 2011 at 17:11
  • 1
    If you add a column to it you need to increase the DATABASE_VERSION for onUpgrade() to be called. Commented Mar 19, 2011 at 18:12
  • adding the columns is before running the code its not while the code is running so i just added two columns to the code and it stooped working......!!? Commented Mar 19, 2011 at 18:44
  • @Moe Where are you adding the columns and what's the error? Commented Mar 19, 2011 at 19:45

1 Answer 1

0

You haven't provided your stacktrace or logcat error. However your code also shows some straight forward errors.

  1. public static final String KEY_FROM = "fromtime"; public static final String KEY_TO = "totime";

just look at these final String values and what you have declared inside DATABASE_CREATE :

from text not null, to not null "

The attribute name (from,to) does not match when you are going to use the same String values while using insert/update or whatever.

  1. You are missing a comma inside the DATABASE_CREATE string.

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.