添加和输入Android SQL数据库

时间:2014-07-29 14:12:09

标签: android sql database

我正在开发一个现有的应用程序,它收集一些数据然后写入数据库。

当我尝试将其写入数据库时​​,我的应用程序崩溃了。我确定我在帮手或设置中遗漏了一些东西,但我没有经验可以理解它是什么。

我的帮助程序类如下:

public class TableSubandObAssessment {

    public static final String TAG = "TableSubandObAssessment";

    public static final String KEY_TABLE = "assessments";

    private static final String KEY_APPOINTMENT_ID = "appointmentID";
    private static final String KEY_TYPE = "type";
    private static final String KEY_ASSESSMENT = "assessment";
    private static final String KEY_VARIABLE1 = "variable1";
    private static final String KEY_VARIABLE2 = "vriable2";
    private static final String KEY_VARIABLE3 = "vriable3";
    private static final String KEY_VARIABLE4 = "vriable4";


    private DatabaseHelper helper;
    private final Context context;
    public static SQLiteDatabase db;

    private static final String CREATE_TABLE = 
                "CREATE TABLE " + KEY_TABLE
                + " (" + KEY_APPOINTMENT_ID + " INTEGER PRIMARY KEY NOT NULL, " 
                + KEY_TYPE + " TEXT NOT NULL, " 
                + KEY_ASSESSMENT + " TEXT NOT NULL, "
                + KEY_VARIABLE1 + " TEXT, "
                + KEY_VARIABLE2 + " TEXT NOT NULL, "
                + KEY_VARIABLE3 + " TEXT, "
                + KEY_VARIABLE4 + " INT " + ");";



    public static void createTable(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    public static void dropTable(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS " + KEY_TABLE);
        createTable(db);
    }   


    public TableSubandObAssessment(Context context) {
        this.context = context;
    }

    public TableSubandObAssessment open() throws SQLException {
        helper = new DatabaseHelper(context);
        db = helper.getWritableDatabase();
        return this;
    }

    public void close() {
        helper.close();
    }


      public static long insertTableSubandObAssessment(int appointmentID, String type, String assessment, String variable1, String variable2, String variable3, String variable4) {
            ContentValues values = new ContentValues();
            values.put(KEY_APPOINTMENT_ID, appointmentID);
            values.put(KEY_TYPE, type);
            values.put(KEY_ASSESSMENT, assessment);
            values.put(KEY_VARIABLE1, variable1);
            values.put(KEY_VARIABLE2, variable2);
            values.put(KEY_VARIABLE3, variable3);
            values.put(KEY_VARIABLE4, variable4);
            return db.insert(KEY_TABLE, null, values);
        }
    }

现有的帮助程序如下(我在对表中的引用中添加了):

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "PGAAPPOINTMENTS.db";
    private static final int DATABASE_VERSION = 1;
    private SQLiteDatabase db = null;

    public DatabaseHelper(Context c) {
        super(c, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void openToRead() {
        close();
        db = this.getReadableDatabase();
    }

    public void openToWrite() {
        close();
        db = this.getWritableDatabase();
    }

    public void close() {
        if (db != null) {
            super.close();
            db = null;
        }

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        AppointmentTable.createTable(db);
        PlayerTable.createTable(db);
        PractitionerTable.createTable(db);
        TableRoutine.createTable(db);
        TableDiagnostic.createTable(db);
        TableAssessment.createTable(db);
        TableFollowUp.createTable(db);
        TableNote.createTable(db);
        TableSubandObAssessment.createTable(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        TableRoutine.dropTable(db);
        AppointmentTable.dropTable(db);
        TableDiagnostic.dropTable(db);
        TableAssessment.dropTable(db);
        TableFollowUp.dropTable(db);
        TableNote.dropTable(db);
        PlayerTable.dropTable(db);
        PractitionerTable.dropTable(db);
        TableSubandObAssessment.dropTable(db);
        onCreate(db);
    }
}

我用来设置数据库的方法是:

public void saveToDb(){
             createTable(TableSubandObAssessment.db);
             int a=(int)ActivityMain.apID;
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Observation", Active_Movements.obs, "","","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Joint Integrity Test", Active_Movements.JIT, "","","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Active Movements", Active_Movements.amv, Active_Movements.amv2,Active_Movements.amv3,"");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Passive Movements", Active_Movements.passive, Active_Movements.passive1,Active_Movements.passive2,"");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Repeated Movements", Active_Movements.repm, Active_Movements.repm1,Active_Movements.repm2,"");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Combined Movements", Active_Movements.comb, Active_Movements.comb1,Active_Movements.comb2,"");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Capsular Pattern", Active_Movements.cappat, "","","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Joint Effusion", Active_Movements.joint_eff, "","","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "PPIVMs", Active_Movements.PPIVMS, Active_Movements.PPIVMS2,Active_Movements.PPIVMS3,Active_Movements.PPIVMS4);
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Other Joints Involved", Active_Movements.other_joints, Active_Movements.other_joints1,"","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Muscle Strength", Active_Movements.muscst, Active_Movements.muscst1,Active_Movements.muscst2,"");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Muscle Control",Active_Movements.musccont, "","","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Muscle Length", Active_Movements.musclength, Active_Movements.musclength1,Active_Movements.musclength2,"");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Isometric Muscle Tests",Active_Movements.imt, "", "","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Integrity Of Nervous System", Active_Movements.INS, Active_Movements.INS1,Active_Movements.INS2,"");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Mobility Of Nervous System", Active_Movements.MobNS, Active_Movements.MobNS1,Active_Movements.MobNS2,"");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Diagnostic Tests", Active_Movements.diagt, "","","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Special Tests", Active_Movements.spect, "","","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Accessory Movements", Active_Movements.accm, "","","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Palpation", Active_Movements.palp, "","","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Conclusion", Active_Movements.conc, "","","");
             TableSubandObAssessment.insertTableSubandObAssessment(a, "Objective", "Diagnosis", Active_Movements.Diag, Active_Movements.Diag1,Active_Movements.Diag2,"");

         }

这里是logcat(我没有想法如何解释):

07-27 11:37:16.757: E/AndroidRuntime(12807): FATAL EXCEPTION: main
07-27 11:37:16.757: E/AndroidRuntime(12807): java.lang.NullPointerException
07-27 11:37:16.757: E/AndroidRuntime(12807):    at com.ucl.pga.db.TableSubandObAssessment.insertTableSubandObAssessment(TableSubandObAssessment.java:73)
07-27 11:37:16.757: E/AndroidRuntime(12807):    at com.ucl.pga.db.Objective.Summary.saveToDb(Summary.java:921)
07-27 11:37:16.757: E/AndroidRuntime(12807):    at com.ucl.pga.db.Objective.Summary$2$1.onClick(Summary.java:495)
07-27 11:37:16.757: E/AndroidRuntime(12807):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
07-27 11:37:16.757: E/AndroidRuntime(12807):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 11:37:16.757: E/AndroidRuntime(12807):    at android.os.Looper.loop(Looper.java:137)
07-27 11:37:16.757: E/AndroidRuntime(12807):    at android.app.ActivityThread.main(ActivityThread.java:4745)
07-27 11:37:16.757: E/AndroidRuntime(12807):    at java.lang.reflect.Method.invokeNative(Native Method)
07-27 11:37:16.757: E/AndroidRuntime(12807):    at java.lang.reflect.Method.invoke(Method.java:511)
07-27 11:37:16.757: E/AndroidRuntime(12807):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-27 11:37:16.757: E/AndroidRuntime(12807):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-27 11:37:16.757: E/AndroidRuntime(12807):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我认为您要写入的属性dbnull。您应该在TableSubandObAssessment.open()之前致电insertTableSubandObAssessment(),因为这会初始化您的属性。请注意,createTable的参数是shadowing您的属性,为插入方法初始化它。