2

I am unable to create a new Database using the API

SQLiteDatabase.openOrCreate(String path,CursorFactory Factory);

When I am using this API, it's throwing an exception

Unable to open database file

Please help me how to fix this exception.

2
  • provide some code you tried... Commented Mar 2, 2011 at 5:29
  • database = SQLiteDatabase.openDatabase(uri.getPath(), null,SQLiteDatabase.CREATE_IF_NECESSARY); Commented Mar 2, 2011 at 5:36

2 Answers 2

2

Suppose you want to create table "Settings" table and "myDB.db". Use this class to create DB and tables

public class MyDbConnector extends SQLiteOpenHelper {
    private static final String TAG = "MyDbConnector";
    public MyDbConnector(Context context) {
        super(context, "myDB.db", null, 3);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
      db.execSQL("CREATE TABLE " + Constants.TABLE_SETTINGS + " ("
              + Constants.TABLE_SETTINGS_FIELD_KEY + " TEXT PRIMARY KEY, "
              + Constants.TABLE_SETTINGS_FIELD_VALUE + " TEXT);"
              );
    }
}

And to Read/Write in DB use,

SQLiteDatabase db = new MyDbConnector(this).getReadableDatabase(); //To read tables in DB
SQLiteDatabase db = new MyDbConnector(this).getWritableDatabase(); // To write something in DB
Sign up to request clarification or add additional context in comments.

2 Comments

I want to open the Database without using the SQLiteOpenHelper Class..is it possible to open/create a database without using the OpenHelper Class?
There are easy way to do same thing, than Why you want to open Database without SQLLiteOpenHelper?
0

I think what you want is this function: getWritableDatabase()

2 Comments

But how to use this method for creating a new database??
@Prachi: Maybe I should have made it clear that you will have to instantiate a SQLiteOpenHelper object first and then you can call that function. Just look at the constructor for that class to see what you need to give it: developer.android.com/reference/android/database/sqlite/…

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.