Issue
public class Database extends SQLiteOpenHelper
{
private SQLiteDatabase database;
public Database(Context context, String name, CursorFactory factory, int version)
{
super(context, name, factory, version);
database = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE USERDATA(name varchar(21), password varchar(13), email varchar(55), score int)");
}
public ArrayList<User> getResults()
{
ArrayList<User> x = new ArrayList<User>();
Cursor cursor = database.rawQuery("SELECT * FROM USERDATA", null);
if(cursor.getCount()==0)
{
cursor.close();
return x;
}
int nameindex = cursor.getColumnIndex("name");
int passwordindex = cursor.getColumnIndex("password");
int emailindex = cursor.getColumnIndex("email");
int scoreindex = cursor.getColumnIndex("score");
cursor.moveToFirst();
do
{
User u;
u = new User(cursor.getString(nameindex), cursor.getString(passwordindex), cursor.getString(emailindex), cursor.getInt(scoreindex));
x.add(u);
}while(cursor.moveToNext());
cursor.close();
return x;
}
public void clearData()
{
database.execSQL("delete from USERDATA");
}
public void insert(String n, String p, String e, int s)
{
database.execSQL("INSERT INTO USERDATA(name, password, email, score) values('"+n+"','"+p+"','"+e+"','"+s+"')");
}
public void update(String e, int s)
{
database.execSQL("UPDATE USERDATA SET score = '"+s+"' where email = '"+e+"'");
}
public boolean select(String e)
{
boolean flag = false;
database.execSQL("SELECT COUNT(*) FROM USERDATA WHERE email = '"+e+"'");
return flag;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE USERDATA");
onCreate(db);
}
public Cursor rawQuery(String query, String[] strings) {
return null;
}
}
^That's the database class. So I used the:
String query = "SELECT COUNT(*) FROM USERDATA WHERE email = ?";
Cursor cursor = db.rawQuery(query, new String[] {x});
if (cursor != null && cursor.moveToFirst()) {
// do your stuff
}
which you told me, in my main class. So to get it working, I gotta do it that way so when Cursor != null, return smth else and not null lol. No matter what now, it'll always return null.
Solution
Try using rawQuery or query api instead of executeSQL which doesn't return anything as its of void type. Try something like:
String query = "SELECT COUNT(*) FROM USERDATA WHERE email = ?";
Cursor cursor = database.rawQuery(query, new String[] {e));
if (cursor != null && cursor.moveToFirst()) {
// do your stuff
}
Answered By - SMA
Answer Checked By - Marilyn (JavaFixing Volunteer)