I'm developping and android quizz app and i have two problems. First i cannot update the questions and second i want to store images in the database with the questions.This my database code Is there someone who can help me? Thanks
package com.example.toureamidou.piste;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
/**
* Created by TOURE Amidou on 24/02/2016.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Question";
private static final String TABLE_QUEST = "quest";
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer";
private static final String KEY_OPTA= "opta";
private static final String KEY_OPTB= "optb";
private static final String KEY_OPTC= "optc";
private SQLiteDatabase dbase;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase=db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)";
db.execSQL(sql);
addQuestions();
}
private void addQuestions() {
Questions q1=new Questions("FFFFF","Tata", "Titi", "Toto", "Tata");
this.addQuestion(q1);
Questions q2=new Questions("Quel est le plus grand pays au monde", "Suisse", "Italie", "Chine", "Chine");
this.addQuestion(q2);
Questions q3=new Questions("Comment s'appele le président francais","Obama", "Hollande","Gorbatchev", "Hollande" );
this.addQuestion(q3);
Questions q4=new Questions("zzzzzzzzzzzzzzzzzzzz", "ali", "dede", "home","dede");
this.addQuestion(q4);
Questions q5=new Questions("Quel est l'homme le plus riche au monde","Gates","Trump","Carlos Slim","Trump");
this.addQuestion(q5);
}
private void addQuestion(Questions quest) {
Log.d("addQuestions", quest.toString());
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQUESTION());
values.put(KEY_ANSWER, quest.getANSWER());
values.put(KEY_OPTA, quest.getOPTA());
values.put(KEY_OPTB, quest.getOPTB());
values.put(KEY_OPTC, quest.getOPTC());
dbase.insert(TABLE_QUEST, null, values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
onCreate(db);
}
public List<Questions> getAllQuestions() {
List<Questions> quesList = new ArrayList<Questions>();
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Questions quest = new Questions();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(2));
quest.setOPTA(cursor.getString(3));
quest.setOPTB(cursor.getString(4));
quest.setOPTC(cursor.getString(5));
quesList.add(quest);
} while (cursor.moveToNext());
}
return quesList;
}
public int updateQuestions (Questions questions){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("questions",questions.getQUESTION());
contentValues.put("answer",questions.getANSWER());
contentValues.put("opta",questions.getOPTA());
contentValues.put("optb",questions.getOPTB());
contentValues.put("optc",questions.getOPTC());
int i = db.update(TABLE_QUEST, contentValues, KEY_ID + " = ?", new String[]{String.valueOf(questions.getID())});
db.close();
return i;
}
}
Aucun commentaire:
Enregistrer un commentaire