有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Sql查询不适用于安卓中的SQLite数据库

我是安卓新手。我想执行一个sql查询,选择id为1到10的数据。我使用的查询在sqlite的DB browser上成功运行,但未在代码中运行。ab和bc是类别目录的静态变量。请帮忙

public class QuizHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION =1;
// Database Name
private static final String DATABASE_NAME = "bcd";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names

private static final String KEY_ID = "qid";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; // correct option
private static final String KEY_OPTA = "opta"; // option a
private static final String KEY_OPTB = "optb"; // option b
private static final String KEY_OPTC = "optc"; // option c

private SQLiteDatabase dbase;

public QuizHelper(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);
    addQuestion();
    // db.close();
}

private void addQuestion() {
    Question q1 = new Question("Who is the president of india ?",   
   "narender modi", "hamid ansari", "pranab mukherji", "pranab   
    mukherji");
    this.addQuestion(q1);
    Question q2 = new Question(" Name of the first university of India   
    ?", "Nalanda University", "Takshshila University", "BHU", "Nalanda  
    University");
    this.addQuestion(q2);
    Question q3 = new Question("Which college is awarded as Outstanding  
    Engineering Institute North Award”?", "Thapar University",  
    "G.N.D.E.C", "S.L.I.E.T", "G.N.D.E.C");
    this.addQuestion(q3);
    Question q4 = new Question("Name of the first Aircraft Carrier Indian 
    Ship ?", "Samudragupt", "I.N.S. Vikrant", "I.N.S Virat", "I.N.S. 
    Vikrant");
    this.addQuestion(q4);
    Question q5 = new Question("In which town of Punjab the largest grain 
    market of Asia is Available?", "Bathinda", "Khanna", "Ludhiana", 
    "Khanna");
    this.addQuestion(q5);
    Question q6 = new Question("Which is the highest dam in India?", 
    "Bhakhra Dam", "Hirakud Dam", "Tehri Dam", "Tehri Dam");
    this.addQuestion(q6);
    Question q7 = new Question("Which Indian state is having longest 
    coastline ?", "Rajasthan", "Gujrat", "Punjab", "Gujrat");
    this.addQuestion(q7);
    Question q8 = new Question("Name of the first Country to print books  
    ?", "China", "India", "USA", "China");
    this.addQuestion(q8);
    Question q9 = new Question("Study of the Universe is known as?", 
    "Sociology", "Cosmology", "Petology", "Cosmology");
    this.addQuestion(q9);
    Question q10 = new Question("Big Bang theory explains ?", "Origin of  
    Universe.", "Origin of Sun", "Laws of physics.", "Origin of 
    Universe.");
    this.addQuestion(q10);
    Question q11 = new Question("Which Planet is dwarf planet?", 
    "Mercury", "Pluto",   "Venus", "Pluto");
    this.addQuestion(q11);
    }
     @Override
     public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
    // Create tables again
    onCreate(db);
}

// Adding new question
public void addQuestion(Question quest) {
    // SQLiteDatabase db = this.getWritableDatabase();
    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());

    // Inserting Row
    dbase.insert(TABLE_QUEST, null, values);
}

public List<Question> getAllQuestions() {
    List<Question> quesList = new ArrayList<Question>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_QUEST + "WHERE" + KEY_ID + "BETWEEN" + catogaries.bc + "AND" +catogaries.ab;
    dbase = this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Question quest = new Question();
            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 quest list
    return quesList;
}

错误日志

  04-05 15:00:28.452 31300-31300/com.example.chaitanya.myquiz   
   E/AndroidRuntime: FATAL EXCEPTION: main

   Process: com.example.chaitanya.myquiz, PID: 31300

   java.lang.RuntimeException: Unable to start activity 

    ComponentInfo
                 {com.example.chaitanya.myquiz.QuestionActivity}:      
                  安卓.database.sqlite.SQLiteException: no such table:   
                 questWHEREqidBETWEEN0AND10 (code 1): , while compiling:    
                 SELECT* FROM questWHEREqidBETWEEN0AND10

  at 
  安卓.app.ActivityThread.performLaunchActivity
  (ActivityThread.java:2305)

at   
安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
                                                                              at 安卓.app.ActivityThread.access$800(ActivityThread.java:147)
                                                                              at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
                                                                              at 安卓.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at 安卓.os.Looper.loop(Looper.java:135)
                                                                              at 安卓.app.ActivityThread.main(ActivityThread.java:5237)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:372)

                                                                              at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:707)
                                                                           Caused by: 安卓.database.sqlite.SQLiteException: no such table: questWHEREqidBETWEEN0AND10 (code 1): , while compiling: SELECT  * FROM questWHEREqidBETWEEN0AND10
                                                                              at 安卓.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

共 (1) 个答案

  1. # 1 楼答案

    关注例外情况:

    no such table: questWHEREqidBETWEEN0AND10

    表名、Where子句和关键字等之间缺少空格。因此,查询将其变成一个完整的字符串

    使用:

    String selectQuery = "SELECT  * FROM " + TABLE_QUEST + " WHERE " + KEY_ID + " BETWEEN " + catogaries.bc + " AND " +catogaries.ab;