Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

android - CRUD: my `find()` function on a SQLite implementation is never executed

In a small app that intercepts SMS and persists them in a local database, I'm trying to implement a find method in my DAO.

this my all my CRUD code :

public class CSmsReceiverDAO implements IDAO<CSmsReceived> {
    private SQLiteDatabase bdd;
    private CMaBaseSQLite cMaBaseSQLite;
    private static final String NOM_BDD = "sms.db";
    private static final int VERSION_BDD = 1;

    private static final String COL_ID = "ID";
    private static final int NUM_COL_ID = 0;

    private static final String TABLE_SMS_SENT = "table_sms_sent";
    private static final String COL_DateSending = "dateSending";

    private static final String TABLE_SMS_RECEIVED = "table_sms_received";
    private static final String COL_DateReceiving = "dateReceiving";

    private static final int NUM_COL_DateReceiving = 1;

    private static final String COL_PROCESSED ="processed";
    private static final int NUM_COL_PROCESSED = 2;

    private static final String COL_PHONE ="phone";
    private static final int NUM_COL_PHONE= 3;

    private static final String COL_DATA ="data";
    private static final int NUM_COL_DATA= 4;


    public CSmsReceiverDAO(Context context){
        //On créer la BDD et sa table
        cMaBaseSQLite = new CMaBaseSQLite(context, NOM_BDD, null, VERSION_BDD);
    }

    public void open(){
        //on ouvre la BDD en écriture
        bdd = cMaBaseSQLite.getWritableDatabase();
    }

    public void close(){
        //on ferme l'accès à la BDD
        bdd.close();
    }

    public SQLiteDatabase getBDD(){
        return bdd;
    }

    @Override
    public CSmsReceived find(String date) throws Exception {
        //Récupère dans un Cursor les valeur correspondant à un livre contenu dans la BDD (ici on sélectionne le livre grace à son titre)
        Cursor c = bdd.query(TABLE_SMS_RECEIVED, new String[] {COL_ID, COL_DateReceiving, COL_PROCESSED, COL_PHONE, COL_DATA}, COL_DateReceiving + " LIKE "" + date +""", null, null, null, null);
        return cursorToSms(c);
    }

    @Override
    public List<CSmsReceived> findAll() throws Exception {
        return null;
    }

    @Override
    public void persist(CSmsReceived t) throws Exception {

        //Création d'un ContentValues (fonctionne comme une HashMap)
        ContentValues values = new ContentValues();
        //on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
        values.put(t.getDateReceiving(), NUM_COL_DateReceiving);
        values.put(t.getProcessed(), NUM_COL_PROCESSED);
        values.put(t.getPhone(), NUM_COL_PHONE);
        values.put(t.getData(), NUM_COL_DATA);

        //on insère l'objet dans la BDD via le ContentValues
        bdd.insert(TABLE_SMS_RECEIVED, null, values);
    }

    @Override
    public void remove(CSmsReceived t) throws Exception {

    }

    @Override
    public void update(CSmsReceived s, CSmsReceived t) throws Exception {

    }

    @Override
    public void refresh(CSmsReceived t) throws Exception {

    }

    //Cette méthode permet de convertir un cursor en un sms
    private CSmsReceived cursorToSms(Cursor c){
        //si aucun élément n'a été retourné dans la requête, on renvoie null
        if (c.getCount() == 0)
            return null;

        //Sinon on se place sur le premier élément
        c.moveToFirst();
        //On créé un sms
        //on lui affecte toutes les infos grace aux infos contenues dans le Cursor
        CSmsReceived cSmsReceived = new CSmsReceived.SmsReceivedBuilder()
                .DateReceiving(c.getString(NUM_COL_DateReceiving))
                .Processed(c.getString(NUM_COL_PROCESSED)).Phone(c.getString(NUM_COL_PHONE))
                .Data(c.getString(NUM_COL_DATA)).build();
        //On ferme le cursor
        c.close();

        //On retourne le livre
        return cSmsReceived;
    }
}

My Sql Base code:

public class CMaBaseSQLite extends SQLiteOpenHelper {

    private static final String COL_ID = "ID";

    private static final String TABLE_SMS_SENT = "table_sms_sent";
    private static final String COL_DateSending = "dateSending";

    private static final String TABLE_SMS_RECEIVED = "table_sms_received";
    private static final String COL_DateReceiving = "dateReceiving";
    private static final String COL_PROCESSED ="processed";

    private static final String COL_PHONE ="phone";
    private static final String COL_DATA ="data";

    private static final String CREATE_BDD_SMS_RECEIVED = "CREATE TABLE " + TABLE_SMS_RECEIVED + " ("
            + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_DateReceiving + "TEXT, "
            + COL_PROCESSED + "TEXT, " + COL_PHONE + "TEXT, " + COL_DATA + "TEXT);";

    private static final String CREATE_BDD_SMS_SENT = "CREATE TABLE " + TABLE_SMS_SENT + " ("
            + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_DateSending + "TEXT, "
            + COL_PHONE + "TEXT, " + COL_DATA + "TEXT NOT NULL);";

    public CMaBaseSQLite(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //on créé la table à partir de la requête écrite dans la variable CREATE_BDD sent et received
        db.execSQL(CREATE_BDD_SMS_RECEIVED);
        //db.execSQL(CREATE_BDD_SMS_SENT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //On peut fait ce qu'on veut ici moi j'ai décidé de supprimer la table et de la recréer
        //comme ?a lorsque je change la version les id repartent de 0
        //db.execSQL("DROP TABLE " + TABLE_SMS_SENT + ";");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SMS_RECEIVED + ";");
        onCreate(db);
    }

it is my activity :

CSmsReceiverDAO cSmsReceiverDAO = new CSmsReceiverDAO(this);
CSmsReceived SmsReceived1 = new CSmsReceived.SmsReceivedBuilder()
        .Data("coords XY").DateReceiving("janvier").Phone("+33565456").Processed("zz").build();

cSmsReceiverDAO.open();

try {
    Log.i("test","try ok");
    cSmsReceiverDAO.persist(SmsReceived1);
    Log.i("test", "persist ok");
    CSmsReceived smsFromBdd = cSmsReceiverDAO.find(SmsReceived1.getDateReceiving());
    Log.i("test", "find ok");

    if (smsFromBdd !=null){
        Toast.makeText(this, smsFromBdd.toString(), Toast.LENGTH_LONG).show();
    }
    else {
        Toast.makeText(this, "persist fail...", Toast.LENGTH_LONG).show();

    }

} catch (Exception e) {
    e.printStackTrace();
}

In my logs, the find method is never executed, but I don't understand why.

02-14 17:25:09.460 7571-7571/com.dev.boblinux.cador I/test: try ok
02-14 17:25:09.470 7571-7571/com.dev.boblinux.cador I/test: persist ok

EDIT: Thanks to Hrundi V. Bakshi:

my logs now:

try ok persist ok find ok

but there is nothing on my smsFromBdd :

        `if (smsFromBdd !=null){
            Toast.makeText(this, smsFromBdd.toString(), Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(this, "persist fail...", Toast.LENGTH_LONG).show();

        }`

displays persist fail...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You aren't separating your field names from their types.
Therefore, the table is malformed.
And your app is aborting when executing cSmsReceiverDAO.find()

You need spaces between the field names and the field types.

private static final String CREATE_BDD_SMS_RECEIVED = "CREATE TABLE " + TABLE_SMS_RECEIVED + " ("
        + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_DateReceiving + " TEXT, "
        + COL_PROCESSED + " TEXT, " + COL_PHONE + " TEXT, " + COL_DATA + " TEXT);";

private static final String CREATE_BDD_SMS_SENT = "CREATE TABLE " + TABLE_SMS_SENT + " ("
        + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_DateSending + " TEXT, "
        + COL_PHONE + " TEXT, " + COL_DATA + " TEXT NOT NULL);";

Then you need to uninstall your app and re-run it, in order for the database to be recreated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...