I´m currently doing a tic tac toe with sqlite, to allow sqlite to save a username with the name and lastname of the person playing but if the username already exists the app will start the tic tac toe game, the thing is, that when I register a username, if I exit the game and try to enter with the same username it send me again to the register form (to write my name and lastname again). My project is being made with API 19 in android studio (KitKat 4.4) I dont know if that could be the reason for sqlite to avoid inserting data. Here is the code for sqlite:
DBHelper.java:
public class DbHelper extends SQLiteOpenHelper {
private static final String DB_NAME="GatoBD.sqlite";
private static final int DB_SCHEME_VERSION=1;
public DbHelper(Context context) {
super(context, DB_NAME, null, DB_SCHEME_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DataBaseManager.CREATE_1_TABLE);
// db.execSQL(DataBaseManager.CREATE_2_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
DataBaseManager.java:
public class DataBaseManager {
public static String TABLE_1_NAME ="Users";
public static String TABLE_2_NAME="Points";
public static final String CN_ID = "_id";
public static final String CN_NAME = "name";
public static final String CN_1_LASTNAME = "last name";
public static final String CN_1_USER = "user";
public static final String CN_2_SCORE = "points";
public static final String CN_2_TIME = "time";
public static final String CREATE_1_TABLE = "create table "+TABLE_1_NAME+ "("//Tabla de usuarios
+ CN_ID + " integer primary key autoincrement,"
+CN_NAME + " text,"
+CN_1_LASTNAME + "text,"
+CN_1_USER +" text);";
public static final String CREATE_2_TABLE = "create table " + TABLE_2_NAME + " ("//Tabla de puntajes
+ CN_ID + " integer primary key autoincrement,"
+CN_1_USER + " text not null,"
+CN_2_SCORE +" integer);"
+CN_2_TIME +" text);";
private DbHelper helper;
private SQLiteDatabase db1;
public DataBaseManager(Context context) {
helper=new DbHelper(context);
db1=helper.getWritableDatabase();
}
public boolean ItsEmpty()
{
String columns[]={CN_ID ,CN_NAME ,CN_1_LASTNAME ,CN_1_USER };
Cursor cursor = db1.query(TABLE_1_NAME, columns, null, null, null, null,null);
if(cursor.getCount()>0)
{
return false; // database not empty
} else {
return true; // database empty
}
}
public ContentValues generateContentValues(String name, String lastname, String user) //
{
ContentValues values=new ContentValues();
valores.put("name",name);
valores.put("last name",lastname);
valores.put("user",user);
return values;
}
public void InsertNewUser(String name, String lastname, String user)
{
db1.insert("Users",null, generateContentValues(name,lastname,user) );
db1.close();
}
public boolean SearchUser(String user)
{
String countQuery = "SELECT * FROM " + TABLE_1_NAME;
Cursor c = db1.rawQuery(countQuery, null);
if (!ItsEmpty())
{
c.moveToFirst();
do {
if (user.equals(c.getString(c.getColumnIndex("user"))))
{
c.close();
return true;
}
}
while (c.moveToNext());
}
c.close();
return false;
}