mardi 10 février 2015

How to add image directlty to sqlite database, using BLOB statement

I have created an Android App, which adds questions to a SQLite database, and then displays them to the user using Radio Buttons and Strings.


This works OK -but now I want to add an image into the database directly, uisng a BLOB key. What is the syntax/path to use in the AddQuestions method, as below?? In other words, what should I enter for the final text/src in the question??


Thanks in advance!



private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "triviaQuiz";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA= "opta"; //option a
private static final String KEY_OPTB= "optb"; //option b
private static final String KEY_OPTC= "optc"; //option c
private static final String KEY_IMAGE_DATA= "image_data";
private SQLiteDatabase dbase;
public DbHelper(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, "+KEY_IMAGE_DATA+" BLOB not null)";
db.execSQL(sql);
addQuestions();
//db.close();
}
private void addQuestions()
{
Question q1;
q1 = new Question("Which company is the largest manufacturer" +
" of network equipment?","HP", "IBM", "CISCO", "C", src="src/main/res/drawable-hdpi/harbour1.png");
this.addQuestion(q1);
Question q2=new Question("Which of the following is NOT " +
"an operating system?", "SuSe", "BIOS", "DOS", "B");
this.addQuestion(q2);
Question q3=new Question("Which of the following is the fastest" +
" writable memory?","RAM", "FLASH","Register","C");
this.addQuestion(q3);
Question q4=new Question("Which of the following device" +
" regulates internet traffic?", "Router", "Bridge", "Hub","A");
this.addQuestion(q4);
Question q5=new Question("Which of the following is NOT an" +
" interpreted language?","Ruby","Python","BASIC","C");
this.addQuestion(q5);
}

Aucun commentaire:

Enregistrer un commentaire