Skip to content
Snippets Groups Projects
Commit a277af82 authored by Boux's avatar Boux
Browse files

Ajout des fonctions SQL d'insertion et de selection des Message

parent 8ffb8564
Branches
1 merge request!1Database
package com.example.qrcode;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.SQLException;
public class Database extends SQLiteOpenHelper implements IDatabase {
......@@ -117,4 +119,92 @@ public class Database extends SQLiteOpenHelper implements IDatabase {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
@Override
public void addMessage(Message message) throws SQLException {
db.execSQL(
"INSERT INTO " + MESSAGE_TABLENAME + "(" +
MESSAGE_COL_IDEMMET + "," +
MESSAGE_COL_IDDEST + "," +
MESSAGE_COL_DATECREATION + "," +
MESSAGE_COL_DATERECEPTION + "," +
MESSAGE_COL_TEXT +
") VALUES (" +
message.getId_emmet() + "," +
message.getId_dest() + "," +
message.getDate_creation() + "," +
message.getDate_reception() + "," +
message.getCorps_de_text() +
")"
);
}
@Override
public Message getMessageByID(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery( "" +
"SELECT " +
MESSAGE_COL_PKID + ", " +
MESSAGE_COL_IDEMMET + ", " +
MESSAGE_COL_IDDEST + ", " +
MESSAGE_COL_DATECREATION + ", " +
MESSAGE_COL_DATERECEPTION + ", " +
MESSAGE_COL_TEXT +
" FROM " + MESSAGE_TABLENAME +
" WHERE " + MESSAGE_COL_PKID + " = " + id
, null);
if(c.moveToFirst()) {
return new Message(
Integer.parseInt(c.getString(0)),
Integer.parseInt(c.getString(1)),
Integer.parseInt(c.getString(2)),
c.getString(3),
c.getString(4),
c.getString(5)
);
}
else {
return null;
}
}
@Override
public Message[] getMessagesByContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery( "" +
"SELECT " +
MESSAGE_COL_PKID + ", " +
MESSAGE_COL_IDEMMET + ", " +
MESSAGE_COL_IDDEST + ", " +
MESSAGE_COL_DATECREATION + ", " +
MESSAGE_COL_DATERECEPTION + ", " +
MESSAGE_COL_TEXT +
" FROM " + MESSAGE_TABLENAME +
" WHERE " +
MESSAGE_COL_IDEMMET + " = " + id +
" OR " + MESSAGE_COL_IDDEST + " = " + id +
" ORDER BY " + MESSAGE_COL_DATECREATION
, null);
if(c.moveToFirst()) {
Message[] messages = new Message[c.getCount()];
int i = 0;
do {
messages[i].fill(
Integer.parseInt(c.getString(0)),
Integer.parseInt(c.getString(1)),
Integer.parseInt(c.getString(2)),
c.getString(3),
c.getString(4),
c.getString(5));
i++;
} while(c.moveToNext());
return messages;
}
else {
return null;
}
}
}
package com.example.qrcode;
import android.database.SQLException;
public interface IDatabase {
// stocker des messages en fonction de pseudo cible
int addMessage(String dest_name, String body);
Message getMessageByID(int id);
Message getMessagesByContact(int id);
void addMessage(Message message) throws SQLException;
Message getMessageByID(int id) throws SQLException;
Message[] getMessagesByContact(int id) throws SQLException;
// stocker image (bin) stocker en fonction du pseudo cible ET pseudo emmeteur.
}
......@@ -19,6 +19,15 @@ public class Message {
this.corps_de_text = corps_de_text;
}
public void fill(int id, int id_emmet, int id_dest, String date_creation, String date_reception, String corps_de_text) {
this.setId(id);
this.setId_emmet(id_emmet);
this.setId_dest(id_dest);
this.setDate_creation(date_creation);
this.setDate_reception(date_reception);
this.setCorps_de_text(corps_de_text);
}
// Getters
public int getId() {
return id;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment