1

Hi I am new to android and I have a problem in creating a database.

public class database extends ListActivity {
    /** Called when the activity is first created. */
    private final String MY_DATABASE_NAME = "myCoolUserDB.db";
    private final String MY_DATABASE_TABLE = "t_Users"; 
    Context c;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<String> results = new ArrayList<String>();
        setContentView(R.layout.main);
        SQLiteDatabase mydb=null;
        try
        {
            mydb.openOrCreateDatabase(MY_DATABASE_NAME,  null);

        } catch(Exception e){}
    }
}

When I run this code it throws a run time exception. Please help me.

1
  • 1
    Your first step should be to print the stack trace of the exception instead of totally ignoring it. And please also fix the formatting of your post. Commented May 23, 2009 at 8:19

4 Answers 4

3
  1. If you are going to call a static method like openOrCreateDatabase, do it on the class (SQLiteDatabase.openOrCreateDatabase(...)), not an instance. It's a lot clearer - the way you've done it looks like you're calling an instance method, so looks like a sure NullPointerException, which of course is misleading.

  2. As someone else has stated, the stack trace would be the most useful thing when asking for help with an exception.

  3. (Almost) never catch an exception without at the very least logging it. Don't just do nothing with it. There are of course exceptions to every rule, but let's not go there for the moment. Anyway, if you don't at least log it, you're just throwing away information that would tell you what went wrong when everything goes to crap later.

  4. You shouldn't be using that method directly, and should instead be extending SQLiteOpenHelper . See the android developers page on data storage to get started (I'd post a link but apparently I'm only allowed one link in my post ?!), and since you've probably had to download the SDK to get going, look in the samples that come with it for the Notepad sample application. That contains a NotePadProvider class, which is a good example of both a content provider and database access, which often go hand-in-hand on android. I'd suggest compiling that application and making some simple changes to it before you jump into making your own one.

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

1 Comment

android developers page on data storage: developer.android.com/guide/topics/data/data-storage.html#db
1

For working with sqlite database you need to create class extended from SQLiteOpenHelper:

private class DBHelper extends SQLiteOpenHelper {   

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

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(UPGRADE_TABLES);
    }

}   

Then you can get access to db using DbHelper object:

DBHelper dbHelper = new DBHelper(Activity.this);
SQLiteDatabase db = dbHelper.getReadableDatabase();

Comments

1

I run into the same problem. It figures out that two bugs happens during development

  • dir "databases" was not existent
  • accendently ".db" was created as directory.

They following code cover both

File dbFile = getDatabasePath ("abc.db");

if (dbFile.isDirectory ()) {
    dbFile.delete();
}
if (! dbFile.exists()) {
    String path = dbFile.getParent ();
    new File (path).mkdirs ();
}
database  = SQLiteDatabase.openDatabase (dbFile.getAbsolutePath (), this, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY);

Hope this helps

I think SQLiteOpenHelper is only useful for "single table" databases. For multiple table applications I consider directly using SQLiteDatabase fit better to a good architecture.

Comments

0

This is a simple post which tells you how to insert data in to a SQLite database in Android and further more this links shows you how to retrieve data from a SQLite database in Android .

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.