I have an embedded database with my app. it’s in the asset folder. The user should be able to add new rows to the database on button click. I have the following code for that:
insertrow.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
db.open();
try
{
String editmacs=editmac.getText().toString();
db.insertrow(editmacs);
// Context context = getApplicationContext();
}
catch (Exception ex)
{
Toast.makeText(getApplicationContext(), "cannot update", Toast.LENGTH_LONG).show();
}
db.close();
}
});
and the relevant method in my SQliteOpenHelper class is:
public static SQLiteDatabase myDb;
public static String DATABASE_TABLE="access_points";
public static final String KEY_column1 = "col1";
public static final String KEY_column2 = "col2";
public static final String KEY_column3= "col3";
public static final String KEY_column4 = "col4";
public long insertrow(String editmacs)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_column1, editmacs);
initialValues.put(KEY_column2, "uf");
initialValues.put(KEY_column3, "ub");
initialValues.put(KEY_column4, "uz");
return myDb.insert(DATABASE_TABLE, null, initialValues);
}
The problem is with button click the editmacs (KEY_column1) is not inserted into the database. But the other values(FOR EXAMPLE “uz”) are inserted(i tested the inserted values with sql query). During insertion using buttonpress i dont get any exception. Can anyone tell why editmacs value from edittext is not inserted? Thanks in advance.
Ok i got the solution from Guillaume’s comments (see below). The queries related to insert are ok. The problem is elsewhere. Look at the following code:
buttonuserentry.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
db.open();
try
{
String mac = "";
mac = db.getuserentry();
Context context = getApplicationContext();
String text = mac;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
catch (Exception ex)
{
Context context = getApplicationContext();
CharSequence text = ex.toString();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
db.close();
}
});
previously, i was using this:
CharSequence mac = "";
mac = db.getuserentry();
Context context = getApplicationContext();
String text = mac;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Because i was using CharSequence, it was showing the last inserted mac address from the original database. The original embedded database declared this column as Text(so that means Text= CharSequence in Sqliteman). My expected mac address is the last typed mac address by the user. This mac address from editmac view is converted to String. So, one must retrieve it as String, not as CharSequence. Thanks again to Guillaume’s help in directing me to the solution.
There’s nothing wrong with your code, it should insert the passed value in the column.
Try to make sure you have a value by adding some kind of debug alert, like a Toast:
And let me know if you get the value you expect. If no (which is my guess), then that’s probably because you read the text value from the wrong
TextView.Are you sure
editmacis correctly initialized from the right view?