Android Open Source - SNET Notes B D D






From Project

Back to project page SNET.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project SNET listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.snet.notepad;
/* w w  w. j  ava 2  s . c  o m*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class NotesBDD
{
    private static final int VERSION_BDD = 1;
    private static final String NOM_BDD = "notes.db";

    private static final String TABLE_NOTES = "table_notes";
    private static final String COL_ID = "ID";
    private static final int NUM_COL_ID = 0;
    private static final String COL_NOTE = "Note";
    private static final int NUM_COL_ISBN = 1;
    private static final String COL_TITRE = "Titre";
    private static final int NUM_COL_TITRE = 2;
    private static final String COL_DATECREATION = "Date_creation";
    private static final int NUM_COL_DATECREATION = 3;
    private static final String COL_DATEMODIFICATION = "Date_modification";
    private static final int NUM_COL_DATEMODIFICATION = 4;

    private SQLiteDatabase bdd;

    private SQLiteBase maBaseSQLite;

    public NotesBDD(Context context)
    {
        //On crer la BDD et sa table
        maBaseSQLite = new SQLiteBase(context, NOM_BDD, null, VERSION_BDD);
    }

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

    public void close()
    {
        //on ferme l'accs  la BDD
        bdd.close();
    }

    public SQLiteDatabase getBDD()
    {
        return bdd;
    }

    public long insertNote(Note note)
    {
        //Cration 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(COL_NOTE, note.getNote());
        values.put(COL_TITRE, note.getTitre());
        values.put(COL_DATECREATION, note.getDateCreation());
        values.put(COL_DATEMODIFICATION, note.getDateModification());
        //on insre l'objet dans la BDD via le ContentValues
        return bdd.insert(TABLE_NOTES, null, values);
    }

    public int updateNote(int id, Note note)
    {
        //La mise  jour d'une note dans la BDD fonctionne plus ou moins comme une insertion
        //il faut simple prciser quelle note on doit mettre  jour grce  l'ID
        ContentValues values = new ContentValues();
        values.put(COL_NOTE, note.getNote());
        values.put(COL_TITRE, note.getTitre());
        values.put(COL_DATEMODIFICATION, note.getDateModification());
        return bdd.update(TABLE_NOTES, values, COL_ID + " = " +id, null);
    }

    public int removeNoteWithID(int id)
    {
        //Suppression d'un livre de la BDD grce  l'ID
        return bdd.delete(TABLE_NOTES, COL_ID + " = " +id, null);
    }

    public Note getNoteWithTitre(String titre)
    {
        //Rcupre dans un Cursor les valeur correspondant  une note contenue dans la BDD (ici on slectionne la note grce  son titre)
        Cursor c = bdd.query(TABLE_NOTES, new String[] {COL_ID, COL_NOTE, COL_TITRE, COL_DATECREATION, COL_DATEMODIFICATION}, COL_TITRE + " LIKE \"" + titre +"\"", null, null, null, null);
        return cursorToNote(c);
    }

    public Note getNoteWithId(int id)
    {
        //Rcupre dans un Cursor les valeur correspondant  un livre contenu dans la BDD (ici on slectionne le livre grce  son titre)
        Cursor c = bdd.query(TABLE_NOTES, new String[] {COL_ID, COL_NOTE, COL_TITRE, COL_DATECREATION, COL_DATEMODIFICATION}, COL_ID + " LIKE " + id +"", null, null, null, null);
        return cursorToNote(c);
    }
    //Cette mthode permet de convertir un cursor en un livre
    private Note cursorToNote(Cursor c)
    {
        //si aucun lment n'a t retourn dans la requte, on renvoie null
        if (c.getCount() == 0)
            return null;

        //Sinon on se place sur le premier lment
        c.moveToFirst();
        //On cr un livre
        Note note = new Note();
        //on lui affecte toutes les infos grce aux infos contenues dans le Cursor
        note.setId(c.getInt(NUM_COL_ID));
        note.setNote(c.getString(NUM_COL_ISBN));
        note.setTitre(c.getString(NUM_COL_TITRE));
        note.setDateCreation(c.getString(NUM_COL_DATECREATION));
        note.setDateModification(c.getString(NUM_COL_DATEMODIFICATION));
        //On ferme le cursor
        c.close();

        //On retourne le livre
        return note;
    }

    public ArrayList<Note> getAllNotes(int tri, boolean ordre)
    {
        ArrayList<Note> noteList = new ArrayList<Note>();
        String selectQuery= null;
        // Select All Query
        if (tri == 1){
          //selectQuery = new String("SELECT  * FROM " + TABLE_NOTES + " ORDER BY ID ");
          selectQuery = new String("SELECT  * FROM " + TABLE_NOTES + " ORDER BY " + COL_DATECREATION +" ");
        }
        else if (tri == 2){
          selectQuery = new String("SELECT  * FROM " + TABLE_NOTES + " ORDER BY " + COL_DATEMODIFICATION +" ");
        }
        else 
        {
          selectQuery = new String("SELECT  * FROM " + TABLE_NOTES + " ORDER BY " + COL_TITRE +" ");
        }
        
        if(ordre == false)
        {
          selectQuery = selectQuery + " DESC";
        }
        
        SQLiteDatabase db = this.maBaseSQLite.getWritableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);
        ;
        // looping through all rows and adding to list
        if (c.moveToFirst())
        {
            do
            {
                Note note = new Note();
                note.setId(c.getInt(NUM_COL_ID));
                note.setNote(c.getString(NUM_COL_ISBN));
                note.setTitre(c.getString(NUM_COL_TITRE));
                note.setDateCreation(c.getString(NUM_COL_DATECREATION));
                Log.v("bdd",c.getString(NUM_COL_DATECREATION));
                note.setDateModification(c.getString(NUM_COL_DATEMODIFICATION));
                // Adding contact to list
                noteList.add(note);
            }
            while (c.moveToNext());
        }

        // return contact list
        c.close();
        return noteList;
    }

    public ArrayList<Note> getSearchedNotes(String str, Boolean contentSearch)
    {
        ArrayList<Note> noteList = new ArrayList<Note>();
        // Select All Query
        SQLiteDatabase db = this.maBaseSQLite.getWritableDatabase();
        String selectQuery = null;
        if(contentSearch == false){
          selectQuery = "SELECT  * FROM " + TABLE_NOTES + " WHERE " +COL_TITRE + " LIKE  \"%" + str+ "%\" ORDER BY ID DESC";
        }
        else
        {
          selectQuery = "SELECT  * FROM " + TABLE_NOTES + " WHERE " +COL_TITRE + " LIKE  \"%" + str+ "%\"" +" OR "  +COL_NOTE + " LIKE  \"%" + str+ "%\" ORDER BY ID DESC";
        }
          Log.i("Requete", selectQuery);
       //Cursor c = this.bdd.query(TABLE_NOTES, new String[] {COL_ID, COL_NOTE, COL_TITRE, COL_DATECREATION}, COL_TITRE + " LIKE \"%" + str+ "%\" ", null, null, null, null);
        Cursor c = db.rawQuery(selectQuery, null);
        
        // looping through all rows and adding to list
        if (c.moveToFirst())
        {  
          Log.i("DB","liste de la req");
            do
            {
                Note note = new Note();
                note.setId(c.getInt(NUM_COL_ID));
                note.setNote(c.getString(NUM_COL_ISBN));
                note.setTitre(c.getString(NUM_COL_TITRE));
                note.setDateCreation(c.getString(NUM_COL_DATECREATION));
                note.setDateModification(c.getString(NUM_COL_DATEMODIFICATION));
                Log.i("DB titre",c.getString(NUM_COL_TITRE));
                // Adding contact to list
                noteList.add(note);
            }
            while (c.moveToNext());
        }

        // return contact list
        c.close();
        return noteList;
    }
    
    // Getting contacts Count
    public int getNotesCount()
    {
        String countQuery = "SELECT  * FROM " + TABLE_NOTES;
        SQLiteDatabase db = this.maBaseSQLite.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }
    
    public void exportDB(){
      File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
           FileChannel source=null;
           FileChannel destination=null;
           String currentDBPath = "/data/"+ "com.snet" +"/databases/"+ NOM_BDD;
           String backupDBPath = "com.snet/" + NOM_BDD;
           //String backupDBPath =  NOM_BDD;
           File currentDB = new File(data, currentDBPath);
           File backupDirDB = new File(sd, "com.snet" );
           backupDirDB.mkdirs();
           
           File backupDB = new File(sd, backupDBPath );
           try {
      backupDB.createNewFile();
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
           
           try {
                source = new FileInputStream(currentDB).getChannel();
                destination = new FileOutputStream(backupDB).getChannel();
                destination.transferFrom(source, 0, source.size());
                source.close();
                destination.close();

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




Java Source Code List

com.snet.MainActivity.java
com.snet.notepad.NoteEdition.java
com.snet.notepad.NoteMain.java
com.snet.notepad.Note.java
com.snet.notepad.NotesBDD.java
com.snet.notepad.Preference.java
com.snet.notepad.SQLiteBase.java
com.snet.textedit.TextEdition.java