我正在尝试插入SQLite数据库。但我得到一个例外,说数据库没有打开。
这是MainActivity.java
的部分 // testing event table.
int id = 1;
int fid = 1;
String name = "intro";
String manager = "Sandeep";
String time = "june 23 6 PM" ;
String venue = "vmcc";
Event e = new Event();
e.setEventId(id);
e.setFestId(fid);
e.setManager(manager);
e.setName(name);
e.setTime(time);
e.setVenue(venue);
EventDatabaseManager edb = new EventDatabaseManager(this);
edb.addEvent(e);
这是EventDatabaseManager.java类的一部分。 我从插入行说java.illegalstateexception得到例外。数据库未打开。
public class EventDatabaseManager extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "events.db";
private static int DATABASE_VERSION = 1;
// table name.
private static final String TABLE_EVENTS = "events";
// column names.
private static final String KEY_ID = "id";
private static final String KEY_FESTID = "festid";
private static final String KEY_NAME = "title";
private static final String KEY_MANAGER = "manager";
private static final String KEY_TIME = "time";
private static final String KEY_VENUE = "venue";
private static final int FESTID_INDEX = 1;
private static final int NAME_INDEX = 2;
private static final int MANAGER_INDEX = 3;
private static final int TIME_INDEX = 4;
private static final int VENUE_INDEX = 5;
public EventDatabaseManager(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database)
{
database.execSQL("DROP TABLE IF EXISTS " + TABLE_EVENTS);
Log.d("database","creating db");
String CREATE_EVENTS_TABLE = "CREATE TABLE " + TABLE_EVENTS
+ "( " + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_FESTID + " INTEGER , "+
KEY_NAME + " TEXT , " + KEY_MANAGER + " TEXT , "+
KEY_TIME + " TEXT , " + KEY_VENUE + " TEXT )";
Log.d("create fn",CREATE_EVENTS_TABLE);
database.execSQL(CREATE_EVENTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Drop older books table if existed
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_EVENTS);
// create fresh books table
this.onCreate(db);
}
/**
* Adding a new event.
*/
public void addEvent(Event e)
{
try{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
/**
* checking to see if the event is already added or not.
*/
ArrayList<Event> list = getAllEvents();
Event testEvent = new Event();
Iterator<Event> in = list.iterator();
while(in.hasNext())
{
testEvent = in.next();
if(testEvent.getEventId() == e.getEventId() && testEvent.getFestId() == e.getFestId())
{
// TODO : showing error.
return;
}
}
values.put(KEY_FESTID, e.getFestId());
values.put(KEY_NAME, e.getName());
values.put(KEY_MANAGER, e.getManager());
values.put(KEY_TIME, e.getTime());
values.put(KEY_VENUE, e.getVenue());
Log.d("adding event" , "values row created.");
// Inserting Row
db.insert(TABLE_EVENTS, null, values);
Log.d("adding event" , "event row added.");
//db.close(); // Closing database connection
}catch(Exception ex)
{
Log.d("adding event" , "exception caught" +ex);
}
}
答案 0 :(得分:0)
您必须创建数据库管理器对象,然后必须在其上调用getWriteableDatabase()
方法。调用此方法时,Android系统将打开您的数据库,如果尚未创建,则会调用onCreate
,或者如果版本已过时,则会调用onUpdate
。