mercredi 21 octobre 2015

Error SQLite android: "Utilities.DBAdapter.open"

I'm using this class for connect with my local DB (sqlite)

/******************** if debug is set true then it will show all Logcat message ************/
public static final boolean DEBUG = true;

/******************** Logcat TAG ************/
public static final String LOG_TAG = "DBAdapter";

/******************** Table Fields ************/
public static final String KEY_ID = "_id";

public static final String KEY_USER_NAME = "user_name";

public static final String KEY_USER_EMAIL = "user_email";


/******************** Database Name ************/
public static final String DATABASE_NAME = "usuarioschachiway.sqlite";

/******************** Database Version (Increase one if want to also upgrade your database) ************/
public static final int DATABASE_VERSION = 1;// started at 1

/** Table names */
public static final String USER_TABLE = "tbl_user";

/******************** Set all table with comma seperated like USER_TABLE,ABC_TABLE ************/
private static final String[] ALL_TABLES = { USER_TABLE };

/** Create table syntax */
private static final String USER_CREATE = "create table if not exists tbl_user(_id integer primary key autoincrement, user_name text not null, user_email text not null);";

/******************** Used to open database in syncronized way ************/
private static DataBaseHelper DBHelper = null;

protected DBAdapter() {
}
/******************* Initialize database *************/
public static void init(Context context) {
    if (DBHelper == null) {
        if (DEBUG)
            Log.i("DBAdapter", context.toString());
        DBHelper = new DataBaseHelper(context);
    }
}

/********************** Main Database creation INNER class ********************/
private static class DataBaseHelper extends SQLiteOpenHelper {
    public DataBaseHelper(Context context) { //constructor (NO TOCAR)
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        if (DEBUG)
            Log.i(LOG_TAG, "new create");
        try {
            db.execSQL(USER_CREATE);


        } catch (Exception exception) {
            if (DEBUG)
                Log.i(LOG_TAG, "Exception onCreate() exception");
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (DEBUG)
            Log.w(LOG_TAG, "Upgrading database from version" + oldVersion
                    + "to" + newVersion + "...");

        for (String table : ALL_TABLES) {
            db.execSQL("DROP TABLE IF EXISTS " + table);
        }
        onCreate(db);
    }

} // Inner class closed


/********************** Open database for insert,update,delete in syncronized manner ********************/
private static synchronized SQLiteDatabase open() throws SQLException {
    return DBHelper.getWritableDatabase();
}


/************************ General functions**************************/


/*********************** Escape string for single quotes (Insert,Update)************/
private static String sqlEscapeString(String aString) {
    String aReturn = "";

    if (null != aString) {
        //aReturn = aString.replace("'", "''");
        aReturn = DatabaseUtils.sqlEscapeString(aString);
        // Remove the enclosing single quotes ...
        aReturn = aReturn.substring(1, aReturn.length() - 1);
    }

    return aReturn;
}
/*********************** UnEscape string for single quotes (show data)************/
private static String sqlUnEscapeString(String aString) {

    String aReturn = "";

    if (null != aString) {
        aReturn = aString.replace("''", "'");
    }

    return aReturn;
}


/********************************************************************/


/**
 * All Operations (Create, Read, Update, Delete)
 */
// Adding new contact

public static void addUserData(String pass_s,String email_S) {
    final SQLiteDatabase db = open();

    String name = sqlEscapeString(pass_s);
    String email = sqlEscapeString(email_S);
    ContentValues cVal = new ContentValues();
    cVal.put(KEY_USER_NAME, name);
    cVal.put(KEY_USER_EMAIL, email);
    db.insert(USER_TABLE, null, cVal);
    db.close(); // Closing database connection
}

// pillar mail y pass
public static List getUserData(int id) {
    final SQLiteDatabase db = open();

    Cursor cursor = db.query(USER_TABLE, new String[]{KEY_ID,
                    KEY_USER_NAME, KEY_USER_EMAIL}, KEY_ID + "=?",
            new String[]{String.valueOf(id)}, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    List<NameValuePair> datosUsuario = new ArrayList<NameValuePair>(); // generar la lista de parametros
    datosUsuario.add(new BasicNameValuePair("id", cursor.getString(0)));
    datosUsuario.add(new BasicNameValuePair("mail",cursor.getString(2)));
    datosUsuario.add(new BasicNameValuePair("pass", cursor.getString(1)));
    // return contact
    return datosUsuario;
}

// update usuario
public static void updateUserData(String id_s, String mail_s, String pass) {
    final SQLiteDatabase db = open();

    ContentValues values = new ContentValues();
    values.put(KEY_USER_NAME, pass);
    values.put(KEY_USER_EMAIL, mail_s);

    // updating row
    db.update(USER_TABLE, values, KEY_ID + " = ?", new String[] { id_s });
}

// eliminar segun su id
public static void deleteUserData(String id_s) {
    final SQLiteDatabase db = open();
    db.delete(USER_TABLE, KEY_ID + " = ?",
            new String[] { id_s });
    db.close();
}

// devuelve el numero de registros
public static int getUserDataCount() {
    String countQuery = "SELECT  * FROM " + USER_TABLE;
    final SQLiteDatabase db = open();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

The class are created well, i think, the problem are here:

private void guardarMYP(String mail_s, String pass_s) {
    int registros = DBAdapter.getUserDataCount();

    if(registros == 0){
        DBAdapter.addUserData(pass_s,mail_s);
    }else{
        DBAdapter.updateUserData("0",mail_s,pass_s);
    }
}

I'm trying use it, but don't detect the DB (res/usuarioschachiway.sqlite).

¿How can access to the method "onCreate" for make the table? ¿Where go the DB sqlite? ¿I must create a folder? ¿How would make an insert in your projects?

¿I must import something? ¿I must put an extends at the mainActivity?

Aucun commentaire:

Enregistrer un commentaire