dyatesupnorth
dyatesupnorth

Reputation: 830

Android database SQLite date may not be null

I am attempting to update my SQLite database with a date field, using a DatePicker. for simplicties sake (on my part) I decided to just take the date and pass it to a String to update the database using just Strings (I have no desire to perform any DATETIME functions in this instance.

The datbase works (at least, via a dialog I set up) however when the data is cast to a TextView nothing appears, also eclipse stacktrace reports the following:

05-09 22:47:30.164: E/SQLiteDatabase(3112): Error inserting chest=100 arms=android.widget.TextView@4125c110 legs=100 waist=100 weight=100
05-09 22:47:30.164: E/SQLiteDatabase(3112): android.database.sqlite.SQLiteConstraintException: personalStats.date may not be NULL (code 19)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at com.uhi.fatfighter.Stats.createEntry(Stats.java:86)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at com.uhi.fatfighter.MainActivity.onClick(MainActivity.java:250)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.view.View.performClick(View.java:4084)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.view.View$PerformClick.run(View.java:16966)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.os.Handler.handleCallback(Handler.java:615)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.os.Looper.loop(Looper.java:137)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at android.app.ActivityThread.main(ActivityThread.java:4928)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at java.lang.reflect.Method.invoke(Method.java:511)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
05-09 22:47:30.164: E/SQLiteDatabase(3112):     at dalvik.system.NativeStart.main(Native Method)

Below is the class where the SQL is handled:

public class Stats {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_WEIGHT = "weight";
    public static final String KEY_WAIST = "waist";
    public static final String KEY_CHEST = "chest";
    public static final String KEY_LEGS = "legs";
    public static final String KEY_ARMS = "arms";
    public static final String KEY_DATE = "date";



    private static final String DATABASE_NAME = "statsDB";
    private static final String DATABASE_TABLE = "personalStats";
    private static final int DATABASE_VERSION = 4;

    private DbHelper ffHelper;
    private final Context ffContext;
    private SQLiteDatabase ffDatabase;

    private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_WEIGHT
                    + " TEXT NOT NULL, " + KEY_WAIST + " TEXT NOT NULL, "
                    + KEY_CHEST + " TEXT NOT NULL, " + KEY_LEGS
                    + " TEXT NOT NULL, " + KEY_ARMS + " TEXT NOT NULL," 
                    + KEY_DATE + " TEXT NOT NULL);");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);

        }
    }

    public Stats(Context c) {
        ffContext = c;
    }

    public Stats open() throws SQLException {
        ffHelper = new DbHelper(ffContext);
        ffDatabase = ffHelper.getWritableDatabase();
        return this;

    }

    public void close() {
        ffHelper.close();

    }

    public long createEntry(String weight, String waist, String chest, String legs, String arms, String date) {


        ContentValues cv = new ContentValues();
        cv.put(KEY_WEIGHT, weight);
        cv.put(KEY_WAIST, waist);
        cv.put(KEY_CHEST, chest);
        cv.put(KEY_LEGS, legs);
        cv.put(KEY_ARMS, arms);
        cv.put(KEY_ARMS, date);

        return ffDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public String getData() {
        String[] columns = new String[] { KEY_ROWID, KEY_WEIGHT, KEY_WAIST, KEY_CHEST, KEY_LEGS, KEY_ARMS, KEY_DATE};
        Cursor c = ffDatabase.query(DATABASE_TABLE, columns, null, null, null,
                null, null);
        String result = "";
        int iRow = c.getColumnIndex(KEY_ROWID);
        int iWeight = c.getColumnIndex(KEY_WEIGHT);
        int iWaist = c.getColumnIndex(KEY_WAIST);
        int iChest = c.getColumnIndex(KEY_CHEST);
        int iLegs = c.getColumnIndex(KEY_LEGS);
        int iArms = c.getColumnIndex(KEY_ARMS);
        int iDate = c.getColumnIndex(KEY_DATE);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result = result + c.getString(iRow) + " " + c.getString(iWeight)
                    + " " + c.getString(iWaist) 
                    + " " + c.getString(iChest) 
                    + " " + c.getString(iLegs) 
                    + " " + c.getString(iArms)
                    + " " + c.getString(iDate)+ "\n";

        }

        return result;
    }
}

The view class:

public class DBView extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_stats);
        TextView tv = (TextView) findViewById(R.id.tvDBInfo);
        Stats dbInfo = new Stats(this);
        dbInfo.open();

        String data = dbInfo.getData();
        dbInfo.close();

            tv.setText(data);



    }

The EditTexts and DatePicker are in my main_activity, i can post if it would help?

Upvotes: 0

Views: 2670

Answers (3)

Alexis C.
Alexis C.

Reputation: 93872

You put two times the same key in your content values.

 cv.put(KEY_ARMS, arms);
    cv.put(KEY_ARMS, date);

Change it with :

    cv.put(KEY_ARMS, arms);
    cv.put(KEY_DATE, date);

Upvotes: 3

Jade Byfield
Jade Byfield

Reputation: 4816

Instead of having your KEY_DATE column set to "TEXT NOT NULL", Just set it to plain "TEXT". You can also do that with any other columns where you may be inserting a null or empty value.

Upvotes: 1

Juraj Ćutić
Juraj Ćutić

Reputation: 206

You didn't insert nothing in KEY_DATE, but KEY_DATE is not null.

Yu must put cv.put(KEY_DATE, date); before insert

Upvotes: 1

Related Questions