Android:how To Write To Sql Database
I have been trying to follow the newBoston tutorial and find myself with a runtime error when I click a button to write data to a database. LogCat displays 'Could not find a method
Solution 1:
LogCat displays "Could not find a method addModuleButton(View) in the activity."
Your button seems to use the XML attribute android:onClick="addModuleButton"
, so simply change this:
publicvoidonClick(View addModuleButton)
into this:
publicvoidaddModuleButton(View view)
From the onClick
documentation:
This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify
android:onClick="sayHello"
, you must declare apublic void sayHello(View v)
method of your context (typically, your Activity).
Solution 2:
Database Class
privatestaticclassDbHelperextendsSQLiteOpenHelper {
publicstaticfinalStringKEY_ROWID="_id";
publicstaticfinalStringKEY_MODULECODE="module_code";
publicstaticfinalStringKEY_MODULENAME="module_name";
publicstaticfinalStringKEY_MODULETYPE="module_type";
publicstaticfinalStringKEY_MODULEDAY="module_day";
publicstaticfinalStringKEY_MODULESTART="module_start";
publicstaticfinalStringKEY_MODULEEND="module_end";
publicstaticfinalStringKEY_MODULELOCATION="module_location";
publicstaticfinalStringKEY_MODULECOMMENTS="module_comments";
privatestaticfinalStringDATABASE_NAME="module_database";
privatestaticfinalStringDATABASE_TABLE="my_modules";
privatestaticfinalintDATABASE_VERSION=1;
private DbHelper myHelper;
privatefinal Context myContext;
private SQLiteDatabase moduleDatabase;
publicDbHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@OverridepublicvoidonCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_MODULECODE + " TEXT NOT NULL, " +
KEY_MODULENAME + " TEXT NOT NULL, " +
KEY_MODULETYPE + " TEXT NOT NULL, " +
KEY_MODULEDAY + " TEXT NOT NULL, " +
//KEY_MODULESTART + " TEXT, " +//KEY_MODULEEND + " TEXT, " +
KEY_MODULELOCATION + " TEXT NOT NULL, " +
KEY_MODULECOMMENTS + " TEXT NOT NULL,);"
);
}
@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABEL IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
publicdatabase(Context c){
myContext = c;
}
public database open()throws SQLException{
myHelper = newDbHelper(myContext);
moduleDatabase = myHelper.getWritableDatabase();
returnthis;
}
publicvoidclose(){
myHelper.close();
}
publiclongcreateEntry(String moduleCode, String moduleName, String moduleType,
String moduleDay, String moduleLocation, String moduleComments) {
ContentValuescv=newContentValues();
cv.put(KEY_MODULECODE,moduleCode);
cv.put(KEY_MODULENAME,moduleName);
cv.put(KEY_MODULETYPE,moduleType);
cv.put(KEY_MODULEDAY,moduleDay);
cv.put(KEY_MODULELOCATION,moduleLocation);
cv.put(KEY_MODULECOMMENTS,moduleComments);
return moduleDatabase.insert(DATABASE_TABLE,null,cv);
// TODO Auto-generated method stub
}
module Activity
DbHelper myHelper= newDbHelper (this, DATABASENAME, null, VERSION_CODE);
SQLiteDatabasedb= myHelper.getWritableDatabase();
ContentValuesvalues=newContentValues();
values.put("module_code", "XXX");
values.put("module_name", "XXX");
...
db.insert("module_database", "", values);
Post a Comment for "Android:how To Write To Sql Database"