0
我在android上工作,我對它很陌生。我想填寫入學表格並將詳細信息存儲在SQLite數據庫中。我無法找出代碼不工作的原因。運行此代碼後,當我填寫表單並單擊「添加」按鈕時,輸出爲「無法保存數據」。如何從editText獲取日期並將其存儲在sqlite數據庫中
這是我的代碼: 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;
}
}
什麼錯誤顯示在logcat的 – SSALPHAX
檢查你沒有錯過逗號。查詢字符串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))「; '沒有逗號終止它?請參閱:https://stackoverflow.com/questions/42457575/unable-to-save-data-in-sqlite-database-in-android –
我會建議在字符串中存儲日期,因爲處理它會更容易 –