mardi 23 décembre 2014

App closes on insert query SQLlite

My app closes when i insert this sample records for testing, i tried it in many ways. from activity class im sending string values to insert to the database.


My Activity Class



if ( mydb.insertsample( "one" , "two" , "3" )){
Toast.makeText(getApplicationContext(), "Insert Success!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
}


My database SQLiteOpenHelper class



public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "MyDBName.db";
public static final String TABLE_SAMPLE = "sample";

public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
public void onCreate(SQLiteDatabase db) {

String CREATE_SAMPLE_TABLE = "CREATE TABLE sample ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"one TEXT, "+
"two TEXT, "+
"three TEXT )";
db.execSQL(CREATE_SAMPLE_TABLE);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS sample");
onCreate(db);
}
public boolean insertsample (String one, String two, String three)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues myValues = new ContentValues();

myValues.put(one, one);
myValues.put(two, two);
myValues.put(three, three);

db.insert(TABLE_SAMPLE, null, myValues);
return true;
}
}

Aucun commentaire:

Enregistrer un commentaire