Reputation: 466
I have an error in one class and I don't know why. The error is:
The method openOrCreateDatabase(String, int, null) is undefined for the type Bdoh
The code:
public class Bdoh extends SQLiteOpenHelper
{
String bdcons = "CREATE TABLE lugares (_id INTEGER PRIMARY KEY AUTOINCREMENT,
nombre TEXT, descripcion TEXT, latitud DOUBLE, longitud DOUBLE, foto TEXT)";
SQLiteOpenHelper sbd;
SQLiteDatabase db=openOrCreateDatabase("lugares",Context.MODE_PRIVATE,null);
Cursor cr;
String TBD="lugares";
public Bdoh(Context context, String name, CursorFactory factory, int version)
{
super(context, "lugares", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
db.execSQL(bdcons);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS lugares");
//Se crea la nueva versión de la tabla
db.execSQL(bdcons);
}
public void insertar(SQLiteDatabase db) //abre la base de datos e inserta registros
{
db=sbd.getWritableDatabase();
}
public void estabd(SQLiteDatabase db, double latitude, double longitude)
{
String bdcon = "SELECT latitud, longitud from lugares where latitud=lat
AND lng=longitud";
db.execSQL(bdcon);
}
}
I think it's a question of object declared wrong. I tried to solve it typing
SQLiteDatabase db=this.openOrCreateDatabase("lugares",Context.MODE_PRIVATE,null);
But the error is not deleted.
How can I fix it?
Thanks.
Upvotes: 0
Views: 1700
Reputation: 7092
use like that
private static class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
// ---this method will creates database implicitly---
DatabaseHelper(Context context) {
super(context, dnname.DATABASE_NAME, null, 1);
Log.v(TAG + ".DatabaseHelper:", "DatabaseHelper constructor called");
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v(TAG + ".onCreate:", "Empty Database created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.v(TAG + ".onUpgrade:", "onUpgrade called");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
Upvotes: 0
Reputation: 96
The database is created automatically when you call getReadableDatabase()
or getWritableDatabase
You can rewrite your constructor so it only takes the Context
as a paramter, like this:
public Bdoh(Context context) {
super(context, "lugares", null, 1);
}
And when you want to write or read from the database you can just create a Bdoh
(lol) object and ask for a readable or writable database. The first time you call one of those methods the systems creates the database for you and calls the onCreate
method from Bdoh
Upvotes: 0
Reputation: 93842
In your constructor initialize your object.
SQLiteDatabase db;
In the constructor :
db=context.openOrCreateDatabase("lugares",Context.MODE_PRIVATE,null);
Upvotes: 1