每当我尝试阅读答案时,它总是返回null,而所有参数都正确填充。数据库表也正确填充,以便它返回null。
public MidSemAnswers getAnswer(String marksId,int answerID, String subject)
{
openReadable();
String select_query= "SELECT * FROM " +MID_SEM_ANSWERS_TABLE + " WHERE " + KEY_MARKS_ID + " = " + marksId +" AND " + KEY_ANSWER_ID + " = " + answerID +" AND " + KEY_SUBJECT + " = '" + subject + "'" ;
Log.d(LOG, select_query);
Cursor c= m_sqLiteDatabase.rawQuery(select_query,null);
MidSemAnswers midSemAnswers=new MidSemAnswers();
if( c != null && c.moveToFirst())
{
midSemAnswers.setId(c.getInt(c.getColumnIndex(KEY_ID)));
midSemAnswers.setSubject(c.getString(c.getColumnIndex(KEY_SUBJECT)));
midSemAnswers.setAnswer_id(c.getString(c.getColumnIndex(KEY_ANSWER_ID)));
midSemAnswers.setAnswer(c.getString(c.getColumnIndex(KEY_ANSWER)));
}
c.close();
return midSemAnswers;
}
这是整个数据库代码:
public class CDataSource
{
private static final String LOG= "DatabaseHelper";
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "questionWizardBank";
//table_names
private static final String MID_SEM_QUESTION_TABLE = "midSemQuesBank";
private static final String MID_SEM_ANSWERS_TABLE = "midSemAnsBank";
//Common column name
private static final String KEY_ID = "id";
private static final String KEY_SEM_ID = "sem_id";
private static final String KEY_SUB_ID = "sub_id";
private static final String KEY_MARKS_ID = "marks_id";
private static final String KEY_SUBJECT = "subject";
//questions table common column name
private static final String KEY_QUESTION_ID = "question_id";
private static final String KEY_QUESTION = "question";
//answers table common column name
private static final String KEY_ANSWER_ID = "answer_id";
private static final String KEY_ANSWER = "answer";
private static CDataSource m_oInstance;
private static final Object m_oInstanceMonitor;
/**
* It is a static class initializer.
*/
static{
m_oInstanceMonitor=CDataSource.class;
}
private final Context m_context;
private DatabaseHelper m_DatabaseHelper;
private SQLiteDatabase m_sqLiteDatabase;
//table creation statements
//mid sem question table creation statement
private static final String CREATE_TABLE_MID_SEM_QUESTIONS= "CREATE TABLE "
+ MID_SEM_QUESTION_TABLE + "(" + KEY_ID + " INTEGER PRIMARY KEY,"+ KEY_SEM_ID + " INTEGER,"
+ KEY_SUB_ID + " INTEGER," + KEY_MARKS_ID + " INTEGER," + KEY_SUBJECT + " TEXT,"
+ KEY_QUESTION_ID + " INTEGER," + KEY_QUESTION + " TEXT"+")";
//Mid sem answers table creation
private static final String CREATE_TABLE_MID_SEM_ANSWERS="CREATE TABLE "
+ MID_SEM_ANSWERS_TABLE + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_SEM_ID + " INTEGER,"
+ KEY_SUB_ID + " INTEGER," + KEY_MARKS_ID + " INTEGER," + KEY_SUBJECT + " TEXT,"
+ KEY_ANSWER_ID + " INTEGER," + KEY_ANSWER + " TEXT"+")";
/**
* Constructor .
* @param context Respective UI context.
*/
public CDataSource(Context context)
{
this.m_context = context;
}
/**
* The following function checks weather the class is initiated or not,
* if not it creates an instance of the current class.
*
* @param context Respective UI context
* @return instance of CProfileDataSource
*/
public static CDataSource getInstance(Context context){
synchronized(m_oInstanceMonitor) {
if(m_oInstance ==null){
m_oInstance=new CDataSource(context);
return m_oInstance;
}
}
return m_oInstance;
}
/**
* The following method determines if the class is initiated then make it's instance null or release the instance.
*/
public static CDataSource releaseInstance(Context context){
synchronized(m_oInstanceMonitor)
{
if (m_oInstance != null)
{
m_oInstance = null;
return null;
}
}
return null;
}
/**
* Open database for insert ,update ,delete or any kind of writable operations.
* @throws SQLException An exception that indicates there was an error with SQL parsing or execution.
*/
private synchronized void openWritable() throws SQLException
{
m_DatabaseHelper = new DatabaseHelper(m_context);
m_sqLiteDatabase = m_DatabaseHelper.getWritableDatabase();
}
/**
* Open database for any kind of readable operations.
* @throws SQLException An exception that indicates there was an error with SQL parsing or execution.
*/
private synchronized SQLiteDatabase openReadable() throws SQLException
{
m_DatabaseHelper = new DatabaseHelper(m_context);
m_sqLiteDatabase = m_DatabaseHelper.getReadableDatabase();
return m_sqLiteDatabase;
}
//add mid sem questions to MID_SEM_QUESTION_TABLE
public void addMidSemQuestions(MidSemQuestions midSemQuestions)
{
openWritable();
m_sqLiteDatabase.beginTransaction();
try {
ContentValues values = new ContentValues();
values.put(KEY_MARKS_ID, midSemQuestions.getMarks_id());
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();
// Log.d(LOG,"Database: "+values);
}catch (Exception e)
{
Log.d(LOG,"Error while trying to add mid sem questions to database");
}finally {
m_sqLiteDatabase.endTransaction();
}
}
//add mid sem answers to MID_SEM_ANSWERS_TABLE
public void addAnswers(MidSemAnswers midSemAnswers)
{
openWritable();
m_sqLiteDatabase.beginTransaction();
try {
ContentValues values = new ContentValues();
values.put(KEY_MARKS_ID, midSemAnswers.getMarks_id());
values.put(KEY_SUBJECT, midSemAnswers.getSubject());
values.put(KEY_ANSWER_ID, midSemAnswers.getAnswer_id());
values.put(KEY_ANSWER, midSemAnswers.getAnswer());
// Notice how we haven't specified the primary key. SQLite auto increments the primary key column.
m_sqLiteDatabase.insertOrThrow(MID_SEM_ANSWERS_TABLE, null, values);
m_sqLiteDatabase.setTransactionSuccessful();
Log.d(LOG,"Database: "+values);
}catch (Exception e)
{
Log.d(LOG,"Error while trying to add mid sem answers to database");
}finally {
m_sqLiteDatabase.endTransaction();
}
}
public List<MidSemQuestions> getQuestionsList(String marks_id, String subject)
{
openReadable();
List<MidSemQuestions> midSemQuestionsList=new ArrayList<MidSemQuestions>();
Cursor cursor = m_sqLiteDatabase.query(MID_SEM_QUESTION_TABLE,
new String [] {KEY_MARKS_ID, KEY_QUESTION_ID, KEY_SUBJECT, KEY_QUESTION },
KEY_MARKS_ID +"=?" +" AND " + KEY_SUBJECT +"=?",
new String[] {marks_id, subject },
null, null, null);
Log.d(LOG,""+cursor);
if(cursor!=null && cursor.moveToFirst())
{
do{
MidSemQuestions midSemQuestions=new MidSemQuestions();
midSemQuestions.setQuestion_id(cursor.getString(cursor.getColumnIndex(KEY_QUESTION_ID)));
midSemQuestions.setQuestion(cursor.getString(cursor.getColumnIndex(KEY_QUESTION)));
Log.d(LOG,"inside cursor"+cursor.getString(cursor.getColumnIndex(KEY_QUESTION)));
//adding to mid sem question list
midSemQuestionsList.add(midSemQuestions);
}while (cursor.moveToNext());
cursor.close();
}
Log.d(LOG,""+midSemQuestionsList);
return midSemQuestionsList;
}
//get single mid sem answer
public MidSemAnswers getMidSemSingleAnswer(long mid_sem_ans_id)
{
openReadable();
String select_query= "SELECT * FROM " +MID_SEM_ANSWERS_TABLE + " WHERE " + KEY_ID + " = " + mid_sem_ans_id;
Log.d(LOG, select_query);
Cursor c= m_sqLiteDatabase.rawQuery(select_query,null);
if(c!=null)
c.moveToFirst();
MidSemAnswers midSemAnswers=new MidSemAnswers();
midSemAnswers.setId(c.getInt(c.getColumnIndex(KEY_ID)));
midSemAnswers.setSubject(c.getString(c.getColumnIndex(KEY_SUBJECT)));
midSemAnswers.setAnswer_id(c.getString(c.getColumnIndex(KEY_ANSWER_ID)));
midSemAnswers.setAnswer(c.getString(c.getColumnIndex(KEY_ANSWER)));
return midSemAnswers;
}
public MidSemAnswers getAnswer(int answerID, String subject)
{
openReadable();
String select_query= "SELECT * FROM " +MID_SEM_ANSWERS_TABLE + " WHERE " + KEY_ANSWER_ID + " = " + answerID +" AND " + KEY_SUBJECT + " = '" + subject + "'" ;
Log.d(LOG, select_query);
Cursor c= m_sqLiteDatabase.rawQuery(select_query,null);
MidSemAnswers midSemAnswers=new MidSemAnswers();
if( c != null && c.moveToFirst())
{
midSemAnswers.setId(c.getInt(c.getColumnIndex(KEY_ID)));
midSemAnswers.setSubject(c.getString(c.getColumnIndex(KEY_SUBJECT)));
midSemAnswers.setAnswer_id(c.getString(c.getColumnIndex(KEY_ANSWER_ID)));
midSemAnswers.setAnswer(c.getString(c.getColumnIndex(KEY_ANSWER)));
}
c.close();
return midSemAnswers;
}
public MidSemAnswers getAnswer(String marksId,int answerID, String subject)
{
openReadable();
String select_query= "SELECT * FROM " +MID_SEM_ANSWERS_TABLE + " WHERE " + KEY_MARKS_ID + " = '" + marksId +"' AND " + KEY_ANSWER_ID + " = '" + answerID +"' AND " + KEY_SUBJECT + " = '" + subject + "'" ;
// String select_query= "SELECT * FROM " +MID_SEM_ANSWERS_TABLE + " WHERE " + KEY_MARKS_ID + " = " + marksId +" AND " + KEY_ANSWER_ID + " = " + answerID +" AND " + KEY_SUBJECT + " = '" + subject + "'" ;
Log.d(LOG, select_query);
Cursor c= m_sqLiteDatabase.rawQuery(select_query,null);
MidSemAnswers midSemAnswers=new MidSemAnswers();
if( c != null && c.moveToFirst())
{
midSemAnswers.setId(c.getInt(c.getColumnIndex(KEY_ID)));
midSemAnswers.setSubject(c.getString(c.getColumnIndex(KEY_SUBJECT)));
midSemAnswers.setAnswer_id(c.getString(c.getColumnIndex(KEY_ANSWER_ID)));
midSemAnswers.setAnswer(c.getString(c.getColumnIndex(KEY_ANSWER)));
}
c.close();
return midSemAnswers;
}
/**
* getting midsem question count
*/
public int getMidSemQuestionCount() {
String countQuery = "SELECT * FROM " + MID_SEM_QUESTION_TABLE;
openReadable();
Cursor cursor = m_sqLiteDatabase.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
return count;
}
// closing database
public void closeDB() {
openReadable();
if (m_sqLiteDatabase != null && m_sqLiteDatabase.isOpen())
m_sqLiteDatabase.close();
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
public DatabaseHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_MID_SEM_QUESTIONS);
db.execSQL(CREATE_TABLE_MID_SEM_ANSWERS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + CREATE_TABLE_MID_SEM_QUESTIONS);
db.execSQL("DROP TABLE IF EXIST " + CREATE_TABLE_MID_SEM_ANSWERS);
onCreate(db);
}
}
}