开发者

How to create a SQLite Database in Android using the API

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 ope开发者_如何学Pythonn database file

Please help me how to fix this exception.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜