gpt4 book ai didi

java - 在数据库中添加图像

转载 作者:行者123 更新时间:2023-11-30 01:31:47 24 4
gpt4 key购买 nike

我正在开发 android 测验应用程序,我有两个问题。首先,我无法更新问题,其次,我想将包含问题的图像存储在数据库中。有几个人回复了我的请求,但是当我尝试按照他们说的去做时,我遇到了一些错误。例如,当我在可绘制文件夹中有图像时,我会在下面的代码中放入哪些行以使我的图像与我的问题一起出现?抱歉,我是 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;
}
}

最佳答案

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

关于java - 在数据库中添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35652273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com