简体   繁体   中英

Android app freezes when trying to use an SQLite database

I am making a 'Talk to the Flash' application which let's you insert a question on a EditText and returns a String as an answer. I've made a database that stored the questions and answers on the same table but on different columns, in which each question and its corresponding answer is on the same row.

My problem is that when I try run the app, it freezes when I press the button which starts to look for the answer for the question typed in.(by using the answerToQuestion method, passing the question as a parameter)

I tried searching for a solution but didn't find (kind of did but didn't understand what it meant).

This is my database handler class (copied everything inside it):

package com.beacon.talktotheflash;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
oimport android.content.Context;

public class MyDBHandler extends SQLiteOpenHelper{

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "flash.db";
private static final String TABLE_QANDA = "questions_and_answers";
private static final String COLUMN_ID = "id";
private static final String COLUMN_QUESTIONS = "questions";
private static final String COLUMN_ANSWERS = "answers";

public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

// Initializes the database
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_QANDA + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_QUESTIONS + " TEXT, " +
COLUMN_ANSWERS + " TEXT);";
db.execSQL(query);
listOfQandA(db);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QANDA);
onCreate(db);
}

// List of the questions and answers
public void listOfQandA(SQLiteDatabase db){
String query = "INSERT INTO " + TABLE_QANDA + " (" + COLUMN_QUESTIONS + ", " + COLUMN_ANSWERS + ") "
+ " VALUES(\"hello\", \"Hi says the Flash!\")";

db.execSQL(query);
}

// Get the answer for the provided question
public String answerToQuestion(String question){
// Check if the question and the answer are in the same row
// by using a cursor
SQLiteDatabase db = getWritableDatabase();
String final_answer = "";
String query1 = "SELECT " + COLUMN_QUESTIONS + " FROM " + TABLE_QANDA + " WHERE 1";
String query2 = "SELECT " + COLUMN_ANSWERS + " FROM " + TABLE_QANDA + " WHERE 1";

// Cursor point to a location in a column
Cursor c1 = db.rawQuery(query1, null);
Cursor c2 = db.rawQuery(query2, null);

// Move to the first row in column_questions
c1.moveToFirst();

while(!c1.isAfterLast()){
if (c1.getString(c1.getColumnIndex("questions")).equals(question)){
int c1_position = c1.getPosition();
c2.moveToPosition(c1_position);
final_answer = c2.getString(c2.getColumnIndex("answers"));
}
}

c1.close();
c2.close();

return final_answer;
}
}

You're infinite looping. You moveToFirst on c1, but you never moveToNext. So you'll never be afterLast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM