Android数据库SQLite日期可能不为null

时间:2013-05-09 21:56:51

标签: java android database sqlite

我正在尝试使用DatePicker更新带有日期字段的SQLite数据库。为了简单起见(就我而言)我决定只使用日期并将其传递给String以仅使用字符串更新数据库(我不想在此实例中执行任何DATETIME函数。

数据库工作(至少通过我设置的对话框)但是当数据被转换为TextView时,没有任何内容出现,eclipse stacktrace也会报告以下内容:

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)

以下是处理SQL的类:

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;
    }
}

视图类:

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);



    }

EditTexts和DatePicker在我的main_activity中,如果有帮助我可以发帖吗?

3 个答案:

答案 0 :(得分:3)

您在内容值中放了两次相同的键。

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

更改为:

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

答案 1 :(得分:1)

您未在KEY_DATE中插入任何内容,但KEY_DATEnot null

Yu必须在插入

之前放置cv.put(KEY_DATE, date);

答案 2 :(得分:1)

不要将KEY_DATE列设置为“TEXT NOT NULL”,只需将其设置为“TEXT”。您也可以对可能插入空值或空值的任何其他列执行此操作。