2015-02-06 3 views
-1

SQLite 데이터베이스를 만들었는데 날짜를 저장하는 방법을 알고 싶습니다.android 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> 

날짜는 실제로 버튼이며 날짜를 저장하지 않고 textviews 만 저장하면됩니다. 그리고 내가 이것을 소개한다면 문자열 날짜 = rs.getString (rs.getColumnIndex (DBHelper.INCOME_COLUMN_DATE)); 그것은 나에게 오류를 준다.

이 당신의 onDateSet() 메소드는 정수로 년, 월, 일을 점점에서 내 로그 캣

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) 
+0

후 로그 캣 스택 트레이스 –

+0

'내가이 String 날짜를 소개합니다 경우 = rs.getString (rs.getColumnIndex (DBHelper.INCOME_COLUMN_DATE)); 왜냐하면 ** 당신은 그것을 정의하지 않았기 때문입니다 **. 이 줄은'String INCOME_COLUMN_DATE = "date";'라고 DBHelper에서 어디에 있습니까? 이 대신에 시도해보십시오 :'String date = rs.getString (rs.getColumnIndex ("date"));' –

답변

0

입니다. Declare 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(); 
    } 
}; 
0

당신은 문자열 데이터 형식을 지원하지 않습니다

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

평방 라이트에 문

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

을 만들 수정하십시오.

또한 커서에서 결과를 가져 오는 것보다 db 메소드를 호출 할 때 null 또는 count를 확인하십시오.

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

나는 그것에 대해 잊어 버렸으나 여전히 내 문제를 해결하지 못한다. –

관련 문제