数据未插入Android应用中的表格中

时间:2014-10-08 05:58:21

标签: android sqlite

我需要在表中插入一些数据,但是当我在表中插入数据时,我在db.insert(Table_Name,null,cm)处获得Null指针异常,声明我无法解析。请发布help.Thanks提前。

package com.example.movietrivia;

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.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class QuizHelper extends SQLiteOpenHelper {

 public static final String Db_Name="Movie.db";
 public static final String Table_Name="MovieTrivia";
 public static int Version_Number=1;
 public static final String Key_Id="Id";
 public static final String key_quest="question";
 public static final String key_ans="Answer";
 public static final String key_opta="OPTA";
 public static final String key_optb="OPTB";
 public static final String key_optc="OPTC";
 public static final String key_optd="OPTD";
 private SQLiteDatabase db;
 public QuizHelper(Context context) {
    super(context, Db_Name, null, Version_Number);

    // this.db = this.getWritableDatabase(); 
    // TODO Auto-generated constructor stub
}
//"Create Table " + table_name + "(" + Col_Name + " TEXT , " +  COL_Age + " TEXT)";
@Override
public void onCreate(SQLiteDatabase d) {
    // TODO Auto-generated method stub



    String query="Create Table " + Table_Name  +  " ("
            + Key_Id + " INTEGER PRIMARY KEY," + key_quest + " TEXT,"
            + key_ans + " TEXT," 
            + key_opta + " TEXT," 
            + key_optb + " TEXT,"
            + key_optc + " TEXT,"
            + key_optd + " TEXT"
            + ");";
    d.execSQL(query);
    Log.d("Query",query);
    addQuestion();
    //Log.d("Query",query);
}
private void addQuestion()
{
    this.getWritableDatabase();
    Question q1=new Question("Who is Jack Sparrow in Pirates of
    Carribean??","dfd","dsdsd","sd","Johnny Depp","D");   
    Log.d("Here","Where");
    this.addQuestions(q1);
    Question q2=new Question("Who plays batman in Dark Knight??","dfd","Christian 
    Bale","jmjjm","dffdfdfd","B");
    this.addQuestions(q2);
    Question q3=new Question("Who pLays Iron Man in the Series??","Robert Downey 
     Jr.","fdfd","nbnbnb","trrrt","A");
    this.addQuestions(q3);
}
 public void addQuestions(Question quest)

{
    //this.getWritableDatabase();
     Log.d("Test","Testing");
     Log.d("key_quest",quest.getQuestions());
     ContentValues cm=new ContentValues();
     Log.d("key_quest",quest.getQuestions());
     Log.d("key_ans",quest.getAnswer());
     Log.d("key_opta",quest.getOPTA());
     Log.d("key_optb",quest.getOPTB());
     Log.d("key_optc",quest.getOPTC());
     Log.d("key_optd",quest.getOPTD());
     cm.put(key_quest,quest.getQuestions());
     cm.put(key_ans,quest.getAnswer());
     cm.put(key_opta,quest.getOPTA());
     cm.put(key_optb,quest.getOPTB());
    cm.put(key_optc,quest.getOPTC());
    cm.put(key_optd,quest.getOPTD());
    db.insert(Table_Name,null,cm);
}

public List<Question> getAllQuestion()
{
    List<Question> quest=new ArrayList<Question>();
     String selectQuery="Select * from " + Table_Name;
     SQLiteDatabase db;
     db=this.getReadableDatabase();
     Cursor cursor=db.rawQuery(selectQuery, null);

      if(cursor.moveToFirst())
    {
         do{
            Question ques=new Question();
            ques.setId(cursor.getInt(0));
            ques.setQuestions(cursor.getString(1));
            ques.setAnswer(cursor.getString(2));
            ques.setOPTA(cursor.getString(3));
            ques.setOPTB(cursor.getString(4));
            ques.setOPTC(cursor.getString(5));
            ques.setOPTD(cursor.getString(6));
        }while(cursor.moveToNext());

    }
    return quest;
}
public int rowcount()
{
int row=0;
String selectQuery = "SELECT  * FROM " + Table_Name;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:0)

您在getWritableDatabase()addQuestion()递归地呼叫onCreate(),而getWritableDatbase()则由addQuestion()触发。

删除SQLiteDatabase来电,或将onCreate()作为参数传递给{{1}}进行处理。