我正在创建一个我遵循给定过程的数据库,但我需要在不同的不同列中插入大量数据,为此我必须在Array中添加值,然后将它们插入到数据库中? ?如果是的话那怎么样?通过这样做我将如何与其他表的数据匹配/检索它们,因为我有更多的表。 任何形式的帮助/建议都会得到真正的赞赏。
将值添加到表中就像:
floor(4.9)
我的模型类就像:
public void addMidSemQuestions(MidSemQuestions midSemQuestions)
{
openWritable();
m_sqLiteDatabase.beginTransaction();
try {
ContentValues values = new ContentValues();
values.put(KEY_SEM_ID, midSemQuestions.getId());
values.put(KEY_SUBJECT, midSemQuestions.getSubject());
values.put(KEY_QUESTION_ID, midSemQuestions.getQuestion_id());
values.put(KEY_QUESTION, midSemQuestions.getQuestion());
// Notice how we haven't specified the primary key. SQLite auto increments the primary key column.
m_sqLiteDatabase.insertOrThrow(MID_SEM_QUESTION_TABLE, null, values);
m_sqLiteDatabase.setTransactionSuccessful();
}catch (Exception e)
{
Log.d(LOG,"Error while trying to add mid sem questions to database");
}finally {
m_sqLiteDatabase.endTransaction();
}
}
}
我需要填写数据,以便我这样做:
public class MidSemQuestions
{
public int id;
public int sem_id;
public int sub_id;
public int marks_id;
public int question_id;
public String subject;
public String question;
public MidSemQuestions() {
}
public MidSemQuestions(int sem_id, int sub_id, int marks_id, int question_id, String subject,String question) {
this.sem_id = sem_id;
this.sub_id = sub_id;
this.marks_id = marks_id;
this.question_id = question_id;
this.subject=subject;
this.question = question;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public int getSub_id() {
return sub_id;
}
public void setSub_id(int sub_id) {
this.sub_id = sub_id;
}
public int getSem_id() {
return sem_id;
}
public void setSem_id(int sem_id) {
this.sem_id = sem_id;
}
public int getMarks_id() {
return marks_id;
}
public void setMarks_id(int marks_id) {
this.marks_id = marks_id;
}
public int getQuestion_id() {
return question_id;
}
public void setQuestion_id(int question_id) {
this.question_id = question_id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
答案 0 :(得分:0)
假设你有这个字段的arraylist,其中包含所有值 您希望使用模型类在数据库中插入。
List<String> sem_id=new ArrayList<>();
List<String> String sub_id=new ArrayList<>();
List<String> String marks_id=new ArrayList<>();
List<String> String question_id=new ArrayList<>();
List<String> Strubg subject=new ArrayList<>();
List<String> String question=new ArrayList<>();
对于保存到数据库,假设,databasehelper.addData()是将数据添加到数据库的数据库对象
for(int i=0;i<sem_id.size();i++{
databasehelper.addData(new CDataSource(this).addMidSemQuestions(
new MidSemQuestions(sub_id.get(i),
marks_id.get(i),question_id.get(i),
subject_id.get(i),question.get(i))));
}
并且要从数据库批量数据中获取,您的数据库应该返回 列表格式,
此处midsem list将从Database
获取所有值List<MidSemQuestions> midsem=new ArrayList<MidSemQuestions>();
midsem=database.getAllFieldsFromDatabase();
for(MidSemQuestions values:midsem){
String sub_id=values.getSub_id();
String sem_id=values.getSem_id();
String marks_id=values.getMarks_id();
String question_id=values.getQuestion_id();
String subject_id=values.getSubject_id();
String question=values.getQuestion();
//if you want array list then, put it in arraylist
//or anything you like
}