我正在开发和Android quizz应用程序,我有两个问题。 首先我无法更新问题,其次我想用问题将图像存储在数据库中。这是我的数据库代码 有人可以帮助我吗? 感谢
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;
}
}
答案 0 :(得分:3)
另一种方法您还可以使用资产文件夹
进行图像加载 //load image
try {
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex)
{
ex.printStackTrace();
}
答案 1 :(得分:1)
为了使数据库记录能够“坚持”#34;你需要参与交易。
db.getWritableDatabase();
db.beginTransaction();
...INSERT, UPDATE, DELETE
if (ok) {
db.setTransactionSuccessful();
}
db.endTransaction();
db.close();
图像可以存储在BLOB列中。
答案 2 :(得分:1)
使用此选项可将图像作为字符串并保存
public String getStringImage() {
if (bitmap != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
} else {
return null;
}
}
答案 3 :(得分:0)
将图像更改为字节{}
ClientManager.connect
将图像插入数据库 ................................................
Bitmap b=BitmapFactory.decodeResource(getResources(),R.drawable.androidpics);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bos);
img=bos.toByteArray();
);