Hi im currently creating an android application that uses a database to store question in, however when attempting to run the game its telling me it cant find my table column D or the right order of the question and answer.But from my point of view i cant see why its wrong.So ive attached the code to see if someone can offer guidance on how to correct this problem ?
package com.example.gregor.disseration;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class QBD extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
//Namee of the database
private static final String DATABASE_NAME = "FUNWITHFLAGS";
//TABLE NAME
private static final String TABLE_FLAG ="flags";
//TABLE COLUMNS NAME
private static final String KEY_ID = "id";
private static final String KEY_Q = "q";
private static final String KEY_A = "a";
private static final String KEY_B = "b";
private static final String KEY_C = "c";
private static final String KEY_D = "d";
private static final String KEY_RA = "ra";
private SQLiteDatabase database;
public QBD(Context context){
super(context,DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
database= db;
String sql = "CREATE TABLE IF NOT EXISTS" + TABLE_FLAG + "("
+ KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_Q
+ " TEXT, " + KEY_RA+ "TEXT,"+KEY_A +"TEXT,"
+KEY_B +"TEXT,"+KEY_C+"TEXT"+ KEY_D+"TEXT)";
db.execSQL(sql);
addQuestions();
db.close();
}
private void addQuestions(){
Quiz q1 = new Quiz("Which one of these flags has 52 Stars?", "USA", "UK", "Germany",
"none of the above","A");
this.addQuestion(q1);
Quiz q2 = new Quiz("Which country has the St George Cross as it flags?", "USA", "England",
"Jamaica ", "none of the above","B");
this.addQuestion(q2);
Quiz q3 = new Quiz("Which one of these countries does NOT have a tricolor?",
"Italy", "UK", "Germany", "France","B");
this.addQuestion(q3);
Quiz q4 = new Quiz("Which one of these flags is NOT in the six nations?", "Tricolore",
" il Tricolore", "Saltire", "Stars and Stripes","D");
this.addQuestion(q4);
Quiz q5 = new Quiz("What does a flag upside down s mean?", "Distress",
" Safety", " A bit of drunken banter ", "None of the above","A");
this.addQuestion(q5);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FLAG);
// Create tables again
onCreate(db);
}
public void addQuestion(Quiz quest){
// SQLiteDatabase db =this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_Q,quest.getQ());
values.put(KEY_RA,quest.getRA());
values.put(KEY_A,quest.getA());
values.put(KEY_B,quest.getB());
values.put(KEY_C,quest.getC());
values.put(KEY_D,quest.getD());
database.insert(TABLE_FLAG,null,values);
}
public List<Quiz>getAllQuestions() {
List<Quiz> quizl = new ArrayList<Quiz>();
String selectQuery = "SELECT * FROM " + TABLE_FLAG;
database = this.getReadableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do {
Quiz qz = new Quiz();
qz.setID(cursor.getInt(0));
qz.setQ(cursor.getString(1));
qz.setRA(cursor.getString(2));
qz.setA(cursor.getString(3));
qz.setB(cursor.getString(4));
qz.setC(cursor.getString(5));
qz.setD(cursor.getString(6));
quizl.add(qz);
} while (cursor.moveToNext());
}
return quizl;
}
public int rowcount(){
int row = 0;
String selectQuery = "SELECT *FROM" + TABLE_FLAG;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row = cursor.getCount();
return row;
}
}
Aucun commentaire:
Enregistrer un commentaire