在数据库中添加图像

时间:2016-02-26 12:53:33

标签: java android sqlite

我正在开发和Android quizz应用程序,我有两个问题。首先我不能更新问题,其次我想用问题将图像存储在数据库中。几个人回复我的请求,但当我尝试做他们说我有一些错误。例如,当我在drawable文件夹中有图像时,我会在下面的代码中添加哪些行来让我的图像出现在我的问题中?对不起,我是Android新手,谢谢

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) {
        SQLiteDatabase dbase = this.getWritableDatabase();
        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;    
    }
}

2 个答案:

答案 0 :(得分:1)

你不能把图像放到SQLDatase中(你可以把它添加为blob,但这在android中不是正确的做法)。您应该将图像的路径或ID保存到数据库而不是图像本身。

答案 1 :(得分:0)

你可以将图像转换为BASE64(基本上是一个字符串),然后将该字符串存储在数据库中。当您想要显示图像检索该字符串转换为图像