如何从editText获取日期并将其存储在sqlite数据库中

时间:2017-05-26 10:40:08

标签: android sqlite date android-calendar

我正在研究android,我是新手。我想填写一份录取表格并将详细信息存储在SQLite数据库中。我无法找出代码无效的原因。运行此代码后输出为"无法保存数据"当我填写表格并点击" ADD"按钮。

这是我的代码: Admission.java

package in.eiskon.suvarnasports;

import android.database.sqlite.SQLiteException;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.ViewGroup;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import java.text.DateFormat;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;

public class Admission extends Fragment implements View.OnClickListener
{
    public int id;
    public String name;
    public String address;
    public Date dob;
    public String contact;
    public Date admission_date;
    public String school;
    public String fees_paid;
    public Calendar c,c1;

    private EditText editTextName;
    private EditText editTextAddress;
    private EditText editTextContact;
    private EditText editTextDOB;
    private EditText editTextAddDOB;
    private EditText editTextSchool;
    private EditText editTextFeespaid;
    private Button btnAdd;
    private SQLiteDatabase database;
    private DatabaseHelper databaseHelper;
    private Admission a;
    private Date d;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        databaseHelper=new DatabaseHelper(getActivity());
        databaseHelper.getWritableDatabase();
        a=new Admission();

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        //Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.admission_fragxml,container,false);

        editTextName=(EditText)view.findViewById(R.id.editTextName);
        editTextAddress=(EditText)view.findViewById(R.id.editTextAddress);
        editTextContact=(EditText)view.findViewById(R.id.editTextContact);
        editTextDOB=(EditText)view.findViewById(R.id.editTextDOB);
        editTextAddDOB=(EditText)view.findViewById(R.id.editTextAddDOB);
        editTextSchool=(EditText)view.findViewById(R.id.editTextSchool);
        editTextFeespaid=(EditText)view.findViewById(R.id.editTextFeespaid);

        btnAdd=(Button)view.findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(this);
        return view;
    }


    @Override
    public void onClick(View v)
    {
         name = editTextName.getText().toString().trim();
         address = editTextAddress.getText().toString().trim();
         contact=editTextContact.getText().toString().trim();
          dob=(Date)editTextDOB.getText();
        //SimpleDateFormat formatter= new SimpleDateFormat("dd/mm/yyyy");
         //dob=formatter.format(editTextDOB.getText());
        //c=Calendar.getInstance();
        //c.setTime(editTextDOB);
        //SimpleDateFormat sdf= new java.text.SimpleDateFormat("dd/MM/yyyy");
        //Date dateObject,admission_date1;
        //try
        //{
         //   String dob=editTextDOB.getText().toString();
          //  dateObject= sdf.parse(dob);
           // String date_dob= new SimpleDateFormat("dd/MM/yyyy").format(dateObject);
          //}
        //catch(ParseException pe)
        //{
         //   pe.printStackTrace();
        //}
       /* SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
        Date dob=sdf.parse(editTextDOB.getText());*/

         //admission_date=formatter.format(editTextAddDOB.getText());
          admission_date=(Date)editTextAddDOB.getText();

        school=editTextSchool.getText().toString().trim();
        fees_paid=editTextFeespaid.getText().toString().trim();
        if(v==btnAdd)
        {
            System.out.println("After if() in admission but..... before insert_successful=database Helper.admission_Insert...");
            boolean insert_Successful=databaseHelper.admission_Insert(a);
            System.out.println("in admission after return....");
            System.out.println("Value of insert_Successful"+insert_Successful);
            if(insert_Successful)
            {
                Toast.makeText(getActivity().getApplicationContext(),"Information Saved Successfully",Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(getActivity().getApplicationContext(),"Unable to Save the information",Toast.LENGTH_LONG).show();
            }
        }
    }


}//Admission class

This is DatabaseHelper class:

package in.eiskon.suvarnasports;

import java.sql.SQLException;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DatabaseHelper extends SQLiteOpenHelper
{
    //database info
    private static final String DATABASE_NAME="Suvarna3DB";
    private static final int DATABASE_VERSION=1;
    public static final String TAG="NAYAN";
    //table names
    private static final String TABLE_FEES_PAID="tab_fees_paid";
   // private static final String TABLE_DETAILS="tab_details";
    private static final String TABLE_ADMISSION="tab_admission";

    //table admission
    private static final String ID="id";
    private static final String NAME="name";
    private static final String ADDRESS="address";
    private static final String DATE_OF_BIRTH="DOB";
    private static final String ADMISSION_DATE="admission_date";
    private static final String CONTACT="contact";
    private static final String SCHOOL="school";
    private static final String PAID_AMOUNT="paid_amount";

    //table fees_paid_#1
    private static final String ID1="id";
    private static final String NAME1="name";
    private static final String DATE_OF_PAYMENT1="date_of_payment";
    private static final String PAID_DATE1="paid_date";
    private static final String PAID_AMOUNT1="paid_amount";

   /* //table details_#2
    private static final String ID2="id";
    private static final String NAME2="name";
    private static final String ADDRESS2="address";
    private static final String ADMISSION_DATE2="admission_date";
    private static final String CONTACT2="contact";
    private static final String DATE_OF_PAYMENT2="date_of_payment";
    private static final String PAID_DATE="paid_date";
    private static final String PAID_AMOUNT2="paid_amount";*/


    public static DatabaseHelper sInstance;
    public static synchronized DatabaseHelper getInstance(Context context)
    {
        if(sInstance==null)
        {
            sInstance=new DatabaseHelper(context.getApplicationContext());
        }
        System.out.println("in DatabaseHelper getInstance()");
        return sInstance;

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

    @Override
    public void onConfigure(SQLiteDatabase db)
    {
        super.onConfigure(db);
        db.setForeignKeyConstraintsEnabled(true);
        System.out.println("in DatabaseHelper onConfigure()");
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        String CREATE_ADMISSION_TABLE="CREATE TABLE "+TABLE_ADMISSION+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" VARCHAR,"+ADDRESS+" VARCHAR,"+DATE_OF_BIRTH+" DATE,"+ADMISSION_DATE+" DATE,"+CONTACT+" VARCHAR,"+SCHOOL+" VARCHAR,"+PAID_AMOUNT+" VARCHAR)";
        //String CREATE_DETAILS_TABLE="CREATE TABLE"+TABLE_DETAILS+"("+ID2+" INTEGER PRIMARY KEY,"+NAME2+" VARCHAR,"+ADDRESS2+" VARCHAR,"+DATE_OF_BIRTH+" DATE,"+ADMISSION_DATE+ "DATE",+CONTACT+" VARCHAR,"+PAID_AMOUNT+" VARCHAR)";
        String CREATE_FEES_PAID_TABLE="CREATE TABLE "+TABLE_FEES_PAID+"("+ID1+" INTEGER,"+NAME1+" VARCHAR,"+DATE_OF_PAYMENT1+" DATE,"+PAID_DATE1+" DATE,"+PAID_AMOUNT1+" VARCHAR, FOREIGN KEY("+ID1+") REFERENCES tab_admission(id))";
        db.execSQL(CREATE_ADMISSION_TABLE);
        db.execSQL(CREATE_FEES_PAID_TABLE);
        System.out.println("in DatabaseHelper onCreate()");
        System.out.println("CREATE TABLE "+TABLE_ADMISSION+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" VARCHAR,"+ADDRESS+" VARCHAR,"+DATE_OF_BIRTH+" DATE,"+ADMISSION_DATE+" DATE,"+CONTACT+" VARCHAR,"+SCHOOL+" VARCHAR,"+PAID_AMOUNT+" VARCHAR)");
    }

    public void onUpgrade(SQLiteDatabase db,int oldVersion, int newVersion)
    {
        if(oldVersion!=newVersion)
        {
            db.execSQL("DROP TABLE IF EXISTS "+TABLE_ADMISSION);
            db.execSQL("DROP TABLE IF EXISTS "+TABLE_FEES_PAID);
            onCreate(db);
            System.out.println("in DatabaseHelper onUpgrade()");

        }
    }

    public boolean admission_Insert(Admission admission)
    {
        SQLiteDatabase db=getWritableDatabase();
        db.beginTransaction();
        System.out.println("in DatabaseHelper admission_Insert() but ... before try block");
        try
        {
            System.out.println("in DatabaseHelper admission_Insert() but ... in try block");
            ContentValues values= new ContentValues();
            values.put(ID,admission.id);
            values.put(NAME,admission.name);
            values.put(ADDRESS,admission.address);
            values.put(DATE_OF_BIRTH,admission.dob);
            values.put(ADMISSION_DATE,admission.admission_date);
            values.put(CONTACT,admission.contact);
            values.put(SCHOOL,admission.school);
            values.put(PAID_AMOUNT,admission.fees_paid);
            System.out.println("in DatabaseHelper admission_Insert() but ... in try block after initialization....");
            boolean insert_Successful=db.insertOrThrow(TABLE_ADMISSION,null,values)>0;
            db.setTransactionSuccessful();
            System.out.println("in DatabaseHelper admission_Insert() but ... in try block before return");
            return insert_Successful;

        }
        catch(Exception e) {
            Log.d(TAG, "Error while trying to perform admission save");
        }
        finally
        {
            db.endTransaction();
        }
        return false;
    }
}

2 个答案:

答案 0 :(得分:0)

在这行代码中:

boolean insert_Successful=databaseHelper.admission_Insert(a);

我没有看到你更新a的值的时刻,它保持不变。

答案 1 :(得分:0)

非常完美的例子希望它对你有用.. check here