'SQLiteDatabase.insert() and SQLiteDatabase.execSQL("insert into ...") doesn't work
I'm working on an Android application in Java. In my application I need, among other things, to store an address in a database. My address is stored in a table named "adresse". This table is defined as below:
CREATE TABLE "adresse" (
"numero_rue" TEXT,
"type_voie" TEXT,
"voie" TEXT,
"code_postal" TEXT,
"ville" TEXT
);
I've tried to insert an address in my table by two different methods:
public void setAdresse(String numRue, String typeVoie, String voie, String codePostal, String ville) {
// First one
ContentValues values = new ContentValues();
values.put("numero_rue" , numRue );
values.put("type_voie" , typeVoie );
values.put("voie" , voie );
values.put("code_postal", codePostal);
values.put("ville" , ville );
db.insert("adresse", null, values);
// Second one
String req = "insert into adresse (numero_rue, type_voie, voie, code_postal, ville) values";
req += "(\"" + numRue + "\",\"" + typeVoie + "\",\"" + voie + "\",\"" + codePostal + "\",\"" + ville + "\")";
db.execSQL(req);
}
I call the setAdresse(...)
method in this method:
public void valider(View paramView) {
[ data recovery without problems ]
final DatabaseAccess db = DatabaseAccess.getInstance(this.getApplicationContext());
db.open();
db.setAdresse(numRue, typeVoie, voie, codePostal, ville);
db.close();
}
I show you my constructor, my open()
, close()
and getInstance()
methods:
private DatabaseAccess(Context context) { this.openHelper = new DatabaseOpenHelper(context); }
public static DatabaseAccess getInstance(Context context) {
if( instance == null ) instance = new DatabaseAccess(context);
return instance;
}
public void open() { this.db = openHelper.getWritableDatabase(); }
public void close() { if( this.db != null ) this.db.close(); }
My problem is that inserting the data into the table is not working. I don't find errors in the logs and my application does not crash. I don't think it is a code error and someone told me it could be linked to my "driver utilisation".
Solution 1:[1]
can you check this link. It is a github sample project for inserting in sqlite table
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Peer Mohamed |