I am asking your help to fix my problem in my android applications. I have a project to make a quiz apps, and I want to implement linear congruential method in this game. I have success to random my data, but I still have no idea how to implement the method. Here is my code;
public class QuizHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mathsone";
// 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 static final String KEY_OPTD = "optd" ;
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, "+ KEY_OPTD + " TEXT)";
db.execSQL(sql);
addQuestion();
// db.close();
}
private void addQuestion() {
Question q1 = new Question("5+2 = ?", "7", "8", "6", "4", "7");
this.addQuestion(q1);
Question q2 = new Question("2+18 = ?", "18", "19", "20", "4", "20");
this.addQuestion(q2);
Question q3 = new Question("10-3 = ?", "6", "7", "8", "5", "7");
this.addQuestion(q3);
Question q4 = new Question("5+7 = ?", "12", "13", "14", "5", "12");
this.addQuestion(q4);
Question q5 = new Question("3-1 = ?", "1", "3", "2", "5", "2");
this.addQuestion(q5);
Question q6 = new Question("0+1 = ?", "1", "0", "10", "5", "1");
this.addQuestion(q6);
Question q7 = new Question("9-9 = ?", "0", "9", "1", "5", "0");
this.addQuestion(q7);
Question q8 = new Question("3+6 = ?", "8", "7", "9", "5", "9");
this.addQuestion(q8);
Question q9 = new Question("1+5 = ?", "6", "7", "5", "5", "6");
this.addQuestion(q9);
Question q10 = new Question("7-5 = ?", "3", "2", "6", "5", "2");
this.addQuestion(q10);
// END
}
#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());
values.put(KEY_OPTD, quest.getOPTD());
// 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 + " ORDER BY RANDOM(), null";
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));
quest.setOPTD(cursor.getString(6));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
}
you can find the formula here
https://id.wikipedia.org/wiki/Metode_linear_kongruen
SQlite Random return pseudo-random integer between -9223372036854775808 and +9223372036854775807.
https://sqlite.org/lang_corefunc.html#randomblob
If you use random() given by android that should solve your problem
Random class uses a 48-bit seed, which is modified using a linear congruential formula.
https://developer.android.com/reference/java/util/Random.html
Change this line
String selectQuery = "SELECT * FROM " + TABLE_QUEST + " ORDER BY RANDOM(), null";
to
Random r = new Random();
int i1 = r.nextInt(MAX_Database_row_Count - Min_Database_row_count) + Min_Database_row_count;
String selectQuery = "SELECT * FROM " + TABLE_QUEST + " ORDER BY "+Integer.toString(i1)+", null";
Related
I've searched a lot for this question but can't seem to actually find an answer. My quiz app works well, but I realized that 50 questions all at once were too many.
Right now, my app works by people clicking a button that says "Begin" and they enter the quiz. After the quiz, all of the questions that they got wrong are shown with the correct answer. However, what I'd like to do is group these into questions of 10. So, select "Question Set 1" Button, "Question Set 2" Button, etc. Then, I'd still like to show the correct answer to the questions contained in this question set.
I'm not actually sure how to separate the questions and have my logic still work. Below I've included by database helper class. I've exempted most of the questions to shorten it. Thank you so much!
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "realEstateQuiz";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "questionId";
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 static final String KEY_OPTD = "optd"; // option d
private SQLiteDatabase dbase;
public DbHelper(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," + KEY_OPTD + " TEXT)";
db.execSQL(sql);
addQuestions(db);
// db.close();
}
private void addQuestions(SQLiteDatabase db) {
Question q1 = new Question(
"When a real estate professional exaggerates " + "about a parcel of property, they are likely:",
"Misrepresenting the property", "Guilty of fraud", "Puffing", "All of the above", "Puffing");
this.addQuestion(q1, db);
}
#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) {
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());
values.put(KEY_OPTD, quest.getOptd());
// Inserting Row
db.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
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.setQuestionId(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));
quest.setOptd(cursor.getString(6));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
cursor.close();
dbase.close();
return quesList;
}
public int rowcount() {
int row = 0;
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row = cursor.getCount();
cursor.close();
return row;
}
}
It seems to me that your Question objects and corresponding table should contain an additional field which indicates which Question Set they belong to. Your DbHelper class should then beable to query Questions by set. Or alternatively you could sort all Questions by their set in the java code, but I would prefer to leave that to the db.
However, If you wish to divide all the questions into sets of 10 you could try something like the following:
List<Set<Question>> questionSets = new ArrayList<Set<Question>>();
Set<Question> questionSet = new HashSet<Question>();
questionSets.add(questionSet);
int counter = 0;
for(Question question : dbHelper.getAllQuestions()){
if(++counter > 10){
counter = 0;
questionSet = new HashSet<Question>();
questionSets.add(questionSet);
}
questionSet.add(question);
}
This will collect ten questions into a Set and the add them to the list of Question Sets. You could then access each question set from that list.
I added a column named id in the database "quest" by using the OnUpgrade() method.But when i run the app there is no data in the arrayList. I used Log for checking the cursor and ArrayList size,Both are zero. When i ran the app first Time it gaves an error of not having a column named id after that it gaves an error of :
java.lang.RuntimeException: Unable to start activity
ComponentInfo
{com.example.chaitanya.myquiz/
com.example.chaitanya.myquiz.QuestionActivity}:
android.database.sqlite.SQLiteException: no such column: id (code 1):
, while compiling: SELECT * FROM quest where id = '1'
I checked the query many time.When i used Select * from +TABLE_QUEST; there are 5 entries in the list and it works fine but not in this case.please help.
public class QuizHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
// 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 static final String KEY_ID2 = "id";
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 " + KEY_ID2 + "
INTEGER)";
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",1);
this.addQuestion(q1);
Question q2 = new Question(" Name of the first university of India ?",
"Nalanda University", "Takshshila University", "BHU", "Nalanda
University",1);
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",1);
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",1);
this.addQuestion(q4);
Question q5 = new Question("In which town of Punjab the largest grain
market of Asia is Available?", "Bathinda", "Khanna", "Ludhiana",
"Khanna",1);
this.addQuestion(q5);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
if (newV > oldV) {
db.execSQL("ALTER TABLE " + TABLE_QUEST + " ADD COLUMN " +
KEY_ID2 + " INTEGER DEFAULT 0");
}
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());
values.put(KEY_ID2,quest.getID());
// 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_ID2 + " = '1' ";
// + KEY_ID2 + " = 1"
dbase = this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
Log.i("here",cursor.getCount()+"");
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
// Log.i("here",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;
}
i am new to android. i want to execute a sql query of selecting data with id 1 to 10.The query i m using is running successfully on DB browser for sqlite but not in the code. ab and bc are static variables of class catogaries.please help.
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;
}
Error Log
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}:
android.database.sqlite.SQLiteException: no such table:
questWHEREqidBETWEEN0AND10 (code 1): , while compiling:
SELECT* FROM questWHEREqidBETWEEN0AND10
at
android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2305)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.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.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Caused by: android.database.sqlite.SQLiteException: no such table: questWHEREqidBETWEEN0AND10 (code 1): , while compiling: SELECT * FROM questWHEREqidBETWEEN0AND10
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
Focus on the Exception:
no such table: questWHEREqidBETWEEN0AND10
You are missing space between table name , Where clause , And keyword etc . so the query makes it a whole string
use:
String selectQuery = "SELECT * FROM " + TABLE_QUEST + " WHERE " + KEY_ID + " BETWEEN " + catogaries.bc + " AND " +catogaries.ab;
This is the database which is initially of 21 question.i added 10 more questions but they are not updated in the database.how i can add them in this database.i am new to android please me with this problem.
public class QuizHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mathsone";
// 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);
Question q12 = new Question(" In South Asia,the country with the largest percentage of aged population is ?", "India", "Sri Lanka", "Nepal", "Sri Lanka");
this.addQuestion(q12);
Question q13 = new Question("Which is the Largest lake of the world ?", "Caspian Sea ", "Dead Sea", "Arabian sea", "Caspian Sea ");
this.addQuestion(q13);
Question q14 = new Question("Which of following industry is famous of Jalandhar city?", "Cycle Parts Industry", "Electronics", "Sports Goods Manufacturing", "Sports Goods Manufacturing");
this.addQuestion(q14);
Question q15 = new Question("Capital of Punjab is:", "Ludhiana", "Amritsar", "Chandigarh", "Chandigarh");
this.addQuestion(q15);
Question q16 = new Question("How many Seats of lok sabha are filled by the candidate of Punjab ?", "12", "17", "13", "13");
this.addQuestion(q16);
Question q17 = new Question("In Malwa and Doaba regions ____ river separates?", " Beas", "Jehlam", "Satluj", "Satluj");
this.addQuestion(q17);
Question q18 = new Question("Which one of the following city of Punjab State was known as Virat Ki Nagri?", "Amritsar", "Dasuha", "Ludhiana", "Dasuha");
this.addQuestion(q18);
Question q19 = new Question("Sachin Tendulkar scored his 100th international century against which’ country ?", "Bangladesh", "Australia", "West Indies", "Bangladesh");
this.addQuestion(q19);
Question q20 = new Question("What is the minimum permissible age for employment in any factory or mine?", "16", "13", "14", "14");
this.addQuestion(q20);
Question q21 = new Question("Where is India's First nuclear centre ?", "Rampur", "Mirapur", "Tarapur", "Tarapur");
this.addQuestion(q21);
Question q22= new Question("Srgsbhs","a","b","cd","cd");
this.addQuestion(q22);
Question q23= new Question("Srgsbhs","a","b","d","d");
this.addQuestion(q23);
Question q24= new Question("Srgsbhs","a","b","f","f");
this.addQuestion(q24);
Question q25= new Question("Srgsbhs","a","b","f","f");
this.addQuestion(q25);
Question q26= new Question("Srgsbhs","a","b","f","f");
this.addQuestion(q26);
Question q27= new Question("Srgsbhs","a","b","f","f");
this.addQuestion(q27);
Question q28= new Question("Srgsbhs","a","b","f","f");
this.addQuestion(q28);
Question q29= new Question("Srgsbhs","a","b","f","f");
this.addQuestion(q29);
Question q30= new Question("Srgsbhs","a","b","f","f");
this.addQuestion(q30);
Question q31= new Question("Srgsbhs","a","b","f","f");
this.addQuestion(q31);
}
#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;
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;
}
}
Invoke addQuestions() at a different place than onCreate() or increase DATABASE_VERSION number to force DB upgrade.
Why is that when i have more than 3 columns the app crashes? I tried debugging it by commenting out one column and it runs perfectly. After debugging it some more I found out that the problem might be on the 'populateListview' because I was able to run the app but now it won't display anything. Why do you think I couldn't run it with more than 3 columns?
Here's my dbAdapter:
// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_INGREDIENTNAME = "ingredientname";
public static final String KEY_IMAGE = "image";
public static final String KEY_DETAILS = "details";
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_INGREDIENTNAME, KEY_IMAGE, KEY_DETAILS};
// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_INGREDIENTNAME = 1;
public static final int COL_IMAGE = 2;
public static final int COL_DETAILS = 3;
//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_INGREDIENTNAME + " TEXT NOT NULL, "
+ KEY_IMAGE + " TEXT"
+ KEY_DETAILS + " TEXT"
+ ");";
// Add a new set of values to be inserted into the database.
public long insertRow(String ingredientname, String image, String detailsValue) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_INGREDIENTNAME, ingredientname);
initialValues.put(KEY_IMAGE, image);
initialValues.put(KEY_DETAILS, detailsValue);
// Insert the data into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}
// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
Here's my populateListView Code:
private void populateListView() {
Cursor cursor = myDb.getAllRows();
String[] fromFieldNames = new String[] { //DBAdapter.KEY_ROWID,
DBAdapter.KEY_INGREDIENTNAME };
int[] toViewIDs = new int[] { //R.id.textViewItemNumber,
R.id.textViewItemTask };
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.item_layout, cursor, fromFieldNames, toViewIDs, 0);
ListView myList = (ListView) findViewById(R.id.listViewTask);
myList.setAdapter(myCursorAdapter);
}
You miss a comma, here:
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_INGREDIENTNAME + " TEXT NOT NULL, "
+ KEY_IMAGE + " TEXT"
+ KEY_DETAILS + " TEXT"
+ ");";
It should be
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_INGREDIENTNAME + " TEXT NOT NULL, "
+ KEY_IMAGE + " TEXT,"
+ KEY_DETAILS + " TEXT"
+ ");";