first of all sorry for my horrible english.
I'm new on android world and now i have so much trouble with sqlite db:
when i try to execute this code:
public void signInOnDB(String name, String surname, String username, String password, String secretQuestion,
String secretAnswer, int g, int m, int a, String regione){
String data = DateTime.setData(g, m, a);
secretAnswer = secretAnswer.toLowerCase();
database.rawQuery("INSERT INTO credenziali (userID, username, password, secretQuestion, secretAnswer) VALUES (null,'" + username + "','" + password + "','" + secretQuestion + "','" +
secretAnswer + "');", null);
Cursor cursor = database.rawQuery("SELECT userID FROM credenziali WHERE username =='"+ username + "';", null);
cursor.moveToFirst();
int id = Integer.parseInt(cursor.getString(0));
cursor.close();
database.rawQuery("INSERT INTO utenti VALUES (" + id + ",'" + name + "','" + surname + "','" + regione + "','" + data + "');", null);
}
the program generate the android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
when he arrive at int id = Integer.parseInt(cursor.getString(0));
I really don't have idea why generate exception, the query can't be null because I insert that value three lines up with the INSERT query.
If I execute that 3 queries on SQLite Browser the returned record is right.
Anyone can help me?
Invoking method:
databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
databaseAccess.signInOnDB(insert_name.getText().toString(), insert_surname.getText().toString(),
insert_username.getText().toString(), insert_password1.getText().toString(), insert_secretQuestion.getText().toString(),
insert_secretAnswer.getText().toString(), g, m, a, regionResidence);
databaseAccess.close();'
Definition of open()/close():
/**
* Open the database connection.
*/
public void open() {
this.database = openHelper.getWritableDatabase();
}
/**
* Close the database connection.
*/
public void close() {
if (database != null) {
this.database.close();
}
}
Definition of openHelper:
public class DatabaseOpenHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "GDB.db";
private static final int DATABASE_VERSION = 1;
public DatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
ok I solved,
when i use INSERT/UPDATE query on database I need to use .execSQL(string)
and not rawQuery(string, ...)