在android sqlite数据库中保存日期

时间:2015-02-06 08:35:12

标签: android sqlite

我创建了一个SQLite数据库,我想知道如何保存日期。

这是我的活动。

public class IncomeActivity extends ActionBarActivity implements View.OnClickListener {

    int from_Where_I_Am_Coming = 0;
    private DBHelper mydb ;
    TextView payer ;
    TextView amount;
    TextView date;
    int id_To_Update = 0;

    DateFormat formate = DateFormat.getDateInstance();
    Calendar calendar = Calendar.getInstance();
    Button label;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income);

        payer = (TextView) findViewById(R.id.input_payer);
        amount = (TextView) findViewById(R.id.input_amount);
        date = (Button) findViewById(R.id.input_date);

        label = (Button) findViewById(R.id.input_date);
        btn = (Button) findViewById(R.id.input_date);
        btn.setOnClickListener(this);
        updatedate();

        mydb = new DBHelper(this);

        Bundle extras = getIntent().getExtras();
        if(extras !=null) {
            int Value = extras.getInt("id");
            if (Value > 0) {
                //means this is the view part not the add contact part.
                Cursor rs = mydb.getData(Value);
                id_To_Update = Value;
                rs.moveToFirst();
                String amo = rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_AMOUNT));
                String pyr = rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_PAYER));
                if (!rs.isClosed()) {
                    rs.close();
                }
                Button save = (Button) findViewById(R.id.btn_save);
                save.setVisibility(View.INVISIBLE);

                Button cancel = (Button) findViewById(R.id.btn_cnc);
                cancel.setVisibility(View.INVISIBLE);

                amount.setText((CharSequence) amo);
                amount.setFocusable(false);
                amount.setClickable(false);

                payer.setText((CharSequence) pyr);
                payer.setFocusable(false);
                payer.setClickable(false);
            }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        Bundle extras = getIntent().getExtras();
        if(extras !=null)
        {
            int Value = extras.getInt("id");
            if(Value>0){
                getMenuInflater().inflate(R.menu.menu_income, menu);
            }
            else{
                getMenuInflater().inflate(R.menu.menu_main, menu);
            }
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        switch(item.getItemId())
        {
            case R.id.Edit_Income:
                Button save = (Button)findViewById(R.id.btn_save);
                save.setVisibility(View.VISIBLE);
                Button cancel = (Button)findViewById(R.id.btn_cnc);
                cancel.setVisibility(View.VISIBLE);

                amount.setEnabled(true);
                amount.setFocusableInTouchMode(true);
                amount.setClickable(true);

                payer.setEnabled(true);
                payer.setFocusableInTouchMode(true);
                payer.setClickable(true);

                return true;
            case R.id.Delete_Income:

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(R.string.deleteIncome)
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                mydb.deleteIncome(id_To_Update);
                                Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(getApplicationContext(),tubapps.datepickerdb.MainActivity.class);
                                startActivity(intent);
                            }
                        })
                        .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // User cancelled the dialog
                            }
                        });
                AlertDialog d = builder.create();
                d.setTitle("Are you sure");
                d.show();

                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
    public void run(View view)
    {
        Bundle extras = getIntent().getExtras();
        if(extras !=null)
        {
            int Value = extras.getInt("id");
            if(Value>0){
                if(mydb.updateIncome(id_To_Update, amount.getText().toString(), payer.getText().toString(), date.getText().toString())){
                    Intent intent = new Intent(getApplicationContext(),tubapps.datepickerdb.MainActivity.class);
                    startActivity(intent);
                }
                else{
                    Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
                }
            }
            else{
                if(mydb.insertIncome(amount.getText().toString(), payer.getText().toString(), date.getText().toString())){
                }
                else{
                }
                Intent intent = new Intent(getApplicationContext(),tubapps.datepickerdb.MainActivity.class);
                startActivity(intent);
            }
        }
    }

    @Override
    public void onClick(View v) {
        SetDate();
    }

    public void updatedate() {
        label.setText(formate.format(calendar.getTime()));
    }

    public void SetDate() {
        new DatePickerDialog(IncomeActivity.this, d, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)).show();
        ContentValues values = new ContentValues();

    }

    DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {


        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthOfYear);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updatedate();
        }
    };
}

这是我的DBHelper。

      public class DBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "MyDBName.db";
    public static final String INCOME_TABLE_NAME = "incomes";
    public static final String INCOME_COLUMN_ID = "id";
    public static final String INCOME_COLUMN_AMOUNT = "amount";
    public static final String INCOME_COLUMN_PAYER = "payer";
    public static final String INCOME_COLUMN_DATE = "date";

    private HashMap hp;


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

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(
                    "create table incomes " +
                            "(id integer primary key, amount string,payer string, date string)"
            );
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS incomes");
            onCreate(db);
        }

        public boolean insertIncome  (String amount, String payer, String date)
        {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();

            contentValues.put("amount", amount);
            contentValues.put("payer", payer);
            contentValues.put("date", date);

            db.insert("incomes", null, contentValues);
            return true;
        }
        public Cursor getData(int id){
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor res =  db.rawQuery( "select * from incomes where id="+id+"", null );
            return res;
        }
        public int numberOfRows(){
            SQLiteDatabase db = this.getReadableDatabase();
            int numRows = (int) DatabaseUtils.queryNumEntries(db, INCOME_TABLE_NAME);
            return numRows;
        }
        public boolean updateIncome (Integer id, String amount, String payer, String date)
        {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("amount", amount);
            contentValues.put("payer", payer);
            contentValues.put("date", date);
            db.update("incomes", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
            return true;
        }

        public Integer deleteIncome (Integer id)
        {
            SQLiteDatabase db = this.getWritableDatabase();
            return db.delete("incomes",
                    "id = ? ",
                    new String[] { Integer.toString(id) });
        }
        public ArrayList getAllIncomes()
        {
            ArrayList array_list = new ArrayList();
            //hp = new HashMap();
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor res =  db.rawQuery( "select * from incomes", null );
            res.moveToFirst();
            while(res.isAfterLast() == false){
                array_list.add(res.getString(res.getColumnIndex(INCOME_COLUMN_PAYER)));
                res.moveToNext();
            }
            return array_list;
        }
    }

这是我的XML。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="tubapps.datepickerdb.IncomeActivity">

    <android.support.v7.widget.CardView
        android:id="@+id/inc_card_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="10dp"
        android:background="#FFFFFF">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/input_amount"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dip"
                android:layout_marginRight="8dip"
                android:hint="@string/income_amount"
                android:inputType="numberSigned|numberDecimal" />

            <EditText
                android:id="@+id/input_payer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/input_amount"
                android:layout_marginLeft="8dip"
                android:layout_marginRight="8dip"
                android:hint="@string/income_payer"
                android:inputType="text" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/date_btn"
                android:background="#FFFFFF"
                android:id="@+id/input_date"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:adjustViewBounds="true"
        android:orientation="horizontal"
        android:showDividers="middle">

        <Button
            android:id="@+id/btn_cnc"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:clickable="true"
            android:focusable="true"
            android:onClick="run"
            android:text="@string/cancel" />

        <Button
            android:id="@+id/btn_save"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:onClick="run"
            android:text="@string/save_income" />
    </LinearLayout>

</RelativeLayout>

日期实际上是一个按钮,当我保存数据库时,它只保存文本视图而不是日期。如果我介绍这个 String date = rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_DATE));它给了我一个错误。

这是我的LogCat

02-06 10:42:10.274    1213-1213/tubapps.datepickerdb E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{tubapps.datepickerdb/tubapps.datepickerdb.IncomeActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
            at android.database.CursorWindow.nativeGetString(Native Method)
            at android.database.CursorWindow.getString(CursorWindow.java:434)
            at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
            at tubapps.datepickerdb.IncomeActivity.onCreate(IncomeActivity.java:65)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

在onDateSet()方法中,您将以年,月和日为整数。声明3字符串日期,月份和年份,然后将int存储在此字符串中。

String date, month, yr

@Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, monthOfYear);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        date = String.valueOf(dayOfMonth);
        month = String.valueOf(monthOfYear);
        yr = String.valueOf(year);
        //now you can put these values in your db like you do it for other strings

        updatedate();
    }
};

答案 1 :(得分:0)

请更正您的创建声明

 "create table incomes " +
                        "(id integer primary key, amount string,payer string, date string)"

 "create table incomes " +
                        "(id INTEGER PRIMARY KEY, amount TEXT,payer TEXT, date TEXT)"

Sq-lite不支持字符串数据类型。

当你从游标获取结果之前调用db方法时也是如此 检查空值或计数。

 Cursor rs = mydb.getData(Value);
            id_To_Update = Value;
            //check for null 
            if(rs!=null && rs.count()>0){
            rs.moveToFirst();
            }