mardi 27 octobre 2015

How to generate TABLES using SqliteDatabse

Sorry for my English. I do not speak very good English language. Being new in android programming, I'm working on an android application that create, modifie, and simply remove quizzes using SQLITEDATABASE.

I get errors after running the program. Here is an explanation of my program

I proceed as follows:

Here is the structure of the Table that i want to create. Table Example

I ask the user to enter the name of the new table to be created for the quiz, and then I take the value entered by the onscreen user in a EDITTEXT and I create a table from the name entered by the user. Here is a picture of the activity_add_quizz. activity_add_quizz

So when a click the button Save NAME. The onclick button method is saveNameListener. The Code is below

Here is the ERROR:

Caused by: android.database.sqlite.SQLiteException: no such table: Table (code 1): , while compiling: SELECT Question FROM Table

The text i have enter in the EditText "Table" was not create as a new TABLE Can someone help me PLZ.

Here is my codes:

public class BasedeDonee extends SQLiteOpenHelper {
public String Table_Name;
SQLiteDatabase db;
public static final int DATABASE_VERSION = 1;
int j = 0;

public String  DATABASE_CREATE1 = "CREATE TABLE "+ Table_Name
        + " (_id integer primary key autoincrement, "
        + "Question TEXT,"
        + "Reponse1 TEXT,"
        + "Reponse2 TEXT,"
        + "Reponse3 TEXT,"
        + "Reponse4 TEXT,"
        + "Reponse5 TEXT,"
        + "Reponse6 TEXT,"
        + "Reponse7 TEXT,"
        + "TrueReponse TEXT);";


public BasedeDonee(Context context) {
    super(context, TableData.TableInfo.DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase database) {
    this.db = database;
    database.execSQL(DATABASE_CREATE1);
    db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2, TrueReponse) VALUES ('First Question','Reponse 1','Reponse 2','Reponse 1')");
    db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2) VALUES ('Second Question','Reponse 1','Reponse 2')");
    db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2) VALUES ('Third Question','Reponse 1', 'Answer 2')");
}


@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        /* Pas pour le moment */
}

public Cursor getTableName() {
    this.db = this.getWritableDatabase();
    return db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' And name is not 'sqlite_sequence' And name is not'android_metadata'", null);

}

public Cursor getTableContent() {
    this.db = this.getWritableDatabase();
    return db.rawQuery("SELECT Question FROM " + Table_Name, null);
}

public Cursor getTableContents() {
    this.db = this.getWritableDatabase();
    return db.rawQuery("SELECT * FROM " + Table_Name, null);
}


public void insertData(String question, String[] table, String reponsevrai) {

    ContentValues cv = new ContentValues();
    cv.put("Question", question);
    // for(int j=0; j<=table.length; j++){
    cv.put("Reponse1", table[0]);
    cv.put("Reponse2", table[1]);
    cv.put("Reponse3", table[2]);
    cv.put("Reponse4", table[3]);
    cv.put("Reponse5", table[4]);
    cv.put("Reponse6", table[5]);
    cv.put("Reponse7", table[6]);

    // }
    cv.put("TrueReponse", reponsevrai);
    db.insert(Table_Name, null, cv);
    Log.d("AddQuizz", "Insertion of Data succed");
}

public void chargerLesQuizzs(List<String> lcs) {

    Cursor c = this.getTableName();

    if (c.moveToFirst()) {
        while (!c.isAfterLast()) {
            lcs.add(c.getString(c.getColumnIndex("name")));
            c.moveToNext();
        }
    }
    c.close();
}

public void chargerLesQuestions(List<String> lcs) {
    Cursor cursor = this.getTableContent();
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        String Question = cursor.getString(0);
        lcs.add(Question);
        cursor.moveToNext();
    }
    cursor.close();
}

public void chargerLesDonees(Question[] q) {
    String[] quest = new String[7];
    Cursor cursor = this.getTableContents();
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        quest[0] = cursor.getString(cursor.getColumnIndex("Reponse1"));
        quest[1] = cursor.getString(cursor.getColumnIndex("Reponse2"));
        quest[2] = cursor.getString(cursor.getColumnIndex("Reponse3"));
        quest[3] = cursor.getString(cursor.getColumnIndex("Reponse4"));
        quest[4] = cursor.getString(cursor.getColumnIndex("Reponse5"));
        quest[5] = cursor.getString(cursor.getColumnIndex("Reponse6"));
        quest[6] = cursor.getString(cursor.getColumnIndex("Reponse7"));

        q[j] = new Question(cursor.getString(cursor.getColumnIndex("Question")), quest, cursor.getString(cursor.getColumnIndex("TrueReponse")));
        j++;

        cursor.moveToNext();
    }
    cursor.close();
}}

Here is a piece of the AddQuizz activity.

public class AddQuizz extends Activity {
String a;
String trueAnswer;
String []reponses={null,null,null,null,null,null,null};
int i=0;

EditText Questions, Reponses, Name;
Button SaveQuestion, Instruction, NextQuestion, SaveReponse;
RadioGroup radios;
RadioButton vrai, faux;

ListView ListSaisi;
List<String> listQuestion = new ArrayList<String>();
ArrayAdapter<String> adapter;
BasedeDonee db;
SQLiteDatabase SQ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_quizz);
    Questions=(EditText)findViewById(R.id.editTextQuestion);
    Reponses=(EditText)findViewById(R.id.editTextReponse);
    Name=(EditText)findViewById(R.id.SaveName);

    Questions.setVisibility(View.INVISIBLE);
    Reponses.setVisibility(View.INVISIBLE);

    SaveQuestion=(Button)findViewById(R.id.SaveQuestion);
    SaveReponse=(Button)findViewById(R.id.SaveReponse);
    Instruction=(Button)findViewById(R.id.buttonInstruction);
    NextQuestion=(Button)findViewById(R.id.buttonQuestionNext);

    SaveQuestion.setVisibility(View.INVISIBLE);
    SaveReponse.setVisibility(View.INVISIBLE);
    SaveQuestion.setVisibility(View.INVISIBLE);
    NextQuestion.setVisibility(View.INVISIBLE);

    vrai=(RadioButton)findViewById(R.id.VraiRadioButton);
    faux=(RadioButton)findViewById(R.id.FauxRadioButton);
    radios=(RadioGroup)findViewById(R.id.group);
    radios.setVisibility(View.INVISIBLE);

    ListSaisi=(ListView) findViewById(R.id.listViewReponses);
    ListSaisi.setVisibility(View.INVISIBLE);





}

public void saveNameListener(View v){

    db=new BasedeDonee(this);
    db.Table_Name=Name.getText().toString();
    db.chargerLesQuestions(listQuestion);


    adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listQuestion);


    ListSaisi.setAdapter(adapter);

    Questions.setVisibility(View.VISIBLE);
    Reponses.setVisibility(View.VISIBLE);
    radios.setVisibility(View.VISIBLE);
    SaveQuestion.setVisibility(View.VISIBLE);
    SaveReponse.setVisibility(View.VISIBLE);
    SaveQuestion.setVisibility(View.VISIBLE);
    NextQuestion.setVisibility(View.VISIBLE);
    ListSaisi.setVisibility(View.VISIBLE);
}}

Aucun commentaire:

Enregistrer un commentaire