Android写入SQLite数据库

时间:2017-09-27 07:10:13

标签: android sqlite

当我尝试写入数据库时​​,我的应用程序崩溃了。

这是我的数据库助手:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "alarms.db";
    private static final String TABLE_NAME = "alarm_table";
    private static final String COL_1 = "ID";
    private static final String COL_2 = "NAME";
    //private static final String COL_3 = "DAYS";
    private static final String COL_4 = "RINGTONE";
    private static final String COL_5 = "HOUR";
    private static final String COL_6 = "MINUTE";

    private static final String TABLE_NAME_DAYS = "days_table";
    private static final String DAYS_COL_1 = "MONDAY";
    private static final String DAYS_COL_2 = "TUESDAY";
    private static final String DAYS_COL_3 = "WEDNESDAY";
    private static final String DAYS_COL_4 = "THURSDAY";
    private static final String DAYS_COL_5 = "FRIDAY";
    private static final String DAYS_COL_6 = "SATURDAY";
    private static final String DAYS_COL_7 = "SUNDAY";

    private SQLiteDatabase db;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db = getWritableDatabase();

        db.execSQL("CREATE TABLE "  + TABLE_NAME + " ("
                + COL_1 + " TEXT PRIMARY KEY NOT NULL, "
                + COL_2 + " TEXT NOT NULL, "
                + COL_4 + " TEXT NOT NULL, "
                + COL_5 + " INTEGER NOT NULL, "
                + COL_6 + " INTEGER NOT NULL" +
        ");");

        db.execSQL("CREATE TABLE "  + TABLE_NAME_DAYS + " ("
                + COL_1 + " TEXT PRIMARY KEY NOT NULL, "
                + DAYS_COL_1 + " TEXT, "
                + DAYS_COL_2 + " TEXT, "
                + DAYS_COL_3 + " TEXT, "
                + DAYS_COL_4 + " TEXT, "
                + DAYS_COL_5 + " TEXT, "
                + DAYS_COL_6 + " TEXT, "
                + DAYS_COL_7 + " TEXT"
                +
        ");");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    }

    public boolean addObjectToDB(Alarm inData){
        boolean rValue = true;
        long result;

        Log.i("This is my super tag!!!", "running from addObjectToDB method!!");
        Log.i("This is my super tag!!!", inData.getName());
        Log.i("This is my super tag!!!", inData.getId());

        // ============== CV : start ==============

        ContentValues cv = new ContentValues();

        cv.put(COL_1, inData.getId());
        cv.put(COL_2, inData.getName());
        //     COL_3 - DAYS
        cv.put(COL_4, inData.getRingtone().toString());
        cv.put(COL_5, inData.getHour());
        cv.put(COL_6, inData.getMinute());

        result = db.insert(TABLE_NAME, null, cv);  //I did some extensive testing and this is the point when the app first crashes

        if(result == -1){
            rValue = false;
        }
        else{
            // ============== CV2 : start ==============

            ContentValues cv2 = new ContentValues();
            List<String> days = inData.getDays();

            cv2.put(COL_1, inData.getId());
            cv2.put(DAYS_COL_1, days.get(0));
            cv2.put(DAYS_COL_2, days.get(1));
            cv2.put(DAYS_COL_3, days.get(2));
            cv2.put(DAYS_COL_4, days.get(3));
            cv2.put(DAYS_COL_5, days.get(4));
            cv2.put(DAYS_COL_6, days.get(5));
            cv2.put(DAYS_COL_7, days.get(6));

            result = db.insert(TABLE_NAME_DAYS, null, cv2);

            rValue = result != -1;
        }

        return rValue;
    }
}

这三种方法来自MainActivity:

@override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myDB = new DatabaseHelper(this);

    ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
    setupViewPager(mViewPager);

    TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
}


@Override
@SuppressWarnings("unchecked")
public void onResume(){
    super.onResume();

    Intent intent = getIntent();
    if(intent.hasExtra("AlarmList")){
        AlarmList = (List<Alarm>) intent.getSerializableExtra("AlarmList");
        Toast.makeText(MainActivity.this, "on resume..!", Toast.LENGTH_SHORT).show();
        testDB();
    }
    else{
        AlarmList = new ArrayList();
    }
}

private void testDB(){
    boolean check = myDB.addObjectToDB(AlarmList.get(0));

    if(check){
        Toast.makeText(MainActivity.this, "Success!", Toast.LENGTH_SHORT).show();
    }
    else{
        Toast.makeText(MainActivity.this, "Failure.. =(", Toast.LENGTH_LONG).show();
    }
}

我尝试检查崩溃日志中可能出现的问题,但是它会显示崩溃的原因,然后快速切换。我确实设法获得了崩溃日志的半截然不错的屏幕截图,它说:

  

无法恢复活动:java.lang.NullPointerException   引起:java.lang.NullPointerException:尝试调用虚拟方法'long android.database.sqlite.SQLiteDatabse.insert(java.lang.String,java.lang.String android.c ...其余如果被切断..

我做错了什么?

1 个答案:

答案 0 :(得分:0)

错过初始化db

margin: 0 auto;