Hi,
I'm trying to develop my first "task manager" application on android
but but this one crash when I open the connection to the Database.
Here you can fin d my code:
DbAdapter:
public class ListDbAdapter {
//Db properties
private static final String DATABASE_NAME = "CUMgr_list.db";
private static final String DATABASE_TABLE = "list";
private static final int DATABASE_VERSION = 1;
//Table properties
private static final String KEY_ID = "_id";
private static final String KEY_TITLE = "title";
private static final String KEY_ITEM = "item";
//Create Script
private static final String DATABASE_CREATE = "create table"
+DATABASE_TABLE+ "(" +KEY_ID+ "integer primary key autoincrement, "
+KEY_TITLE+ "text not null," +KEY_ITEM+ "text not null);";
private SQLiteDatabase db;
private final Context context;
private MyDbHelper myDbHelper;
public ListDbAdapter(Context _context){
this.context = _context;
}
public ListDbAdapter open() throws SQLException {
myDbHelper = new MyDbHelper(context, DATABASE_NAME, null,
DATABASE_VERSION);
db = myDbHelper.getWritableDatabase();
return this;
}
public void close(){
db.close();
}
//Insert Row
public long insertEntry(String sTitle, String sItem) {
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_TITLE, sTitle);
contentValues.put(KEY_ITEM, sItem);
return db.insert(DATABASE_TABLE, null, contentValues);
}
//Remove a row
public boolean removeEntry(long _id){
return db.delete(DATABASE_TABLE, KEY_ID + "=" + _id,null) > 0;
}
//Edit a row
public int updateEntry(long _id, ListItems myItems){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_TITLE, myItems.getTitle());
contentValues.put(KEY_ITEM, myItems.getItem());
return db.update(DATABASE_TABLE, contentValues, KEY_ID + "=" +
_id,
null);
}
//internal class to create and update Db
private static class MyDbHelper extends SQLiteOpenHelper {
public MyDbHelper(Context context, String name, CursorFactory
factory,
int version){
super(context,name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase _db){
_db.execSQL(DATABASE_CREATE);
}
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int
_newVersion){
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(_db);
}
}
}
And the code of my Activity where I open the connection:
myButton.setOnClickListener(new OnClickListener (){
public void onClick(View v){
String sItem = myItem.getText().toString();
String sTitle = myTitle.getText().toString();
myDb.open();//connect to Db
myDb.insertEntry(sTitle, sItem);//insert in Db
myDb.close();//close Db
ListArrayList.add(sItem);
ListArrayAdapter.notifyDataSetChanged();
myItem.setText("");
}
});
I 'll be really great if you could help me.
Cordially,
Etienne
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---