i have created a class for creating database in android, but there are some compilation errors in this..... I am not able to identify why these errors are coming.. My Code is
public DbHelper(Context ctx) {
try {
db = ctx.openDatabase(DATABASE_NAME,null);
} catch (FileNotFoundException e) {
try {
db =
ctx.createDatabase(DATABASE_NAME, DATABASE_VERSION, 0,
null);
db.execSQL(DATABASE_CREATE);
} catch (FileNotFoundException e1) {
db = null;
}
}
}
When i am opening database with "Context" object , it is giving me error that "openDatabase" method is undefined. ?
Check out this link-
http://knightswhocode.com/wordpress/2011/02/android-sqlite-crud-methods-2/
This link explains open and create database methods and how to create database class. Similarly you need to create your class.Try using it!
Add the following at the top of DbHelper() constructor:
this.ctx=ctx //where ctx is the object of Context class (Context ctx);
Check it.
Can you post what lines give what errors exactly?
An Android Context object does not have an openDatabase
method, it does however have an openOrCreateDatabase
method, did you mean to use that instead? Or did you want to use the static openDatabase
method on SQLiteDatabase . so db = SQLiteDatabase.openDatabase(....);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.