2
因爲我是Android開發新手。請告訴我如何將數據從textview添加到數據庫。我已經創建了一個數據庫處理程序類。但無法知道要在我的主要活動中寫些什麼來保存sqlite中的數據。 這是我DBhelper類:我想從textview中添加數據到sqlite數據庫
public class DBHandler extends SQLiteOpenHelper
{
public DBHandler(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
private static final String DB_NAME = "SchedulerDB";
private static final int VERSION = 1;
public static final String TABLE_PERSON = "Person";
private static final String COLUMN_NAME_PERSONNAME = "Name";
private static final String COLUMN_NAME_DATE = "Date";
private static final String COLUMN_NAME_TIME = "Time";
private static final String TABLE_APPOINTMENT_CHECKED = "AppointmentChecked";
private static final String COLUMN_NAME_DATE_LAST_CHECKED = "LastChecked";
private SQLiteDatabase myDB;
public DBHandler(Context context)
{
super(context, DB_NAME, null, VERSION);
myDB = getWritableDatabase();
} // End of constructor
@Override
public void onCreate(SQLiteDatabase db)
{
String createPerson = "CREATE TABLE " + TABLE_PERSON + "(" + COLUMN_NAME_PERSONNAME + " CHAR PRIMARY KEY, " + COLUMN_NAME_DATE + " DATE, "
+ COLUMN_NAME_TIME + " TIME);" ;
db.execSQL(createPerson);
String createAppointmentChecked = "CREATE TABLE " + TABLE_APPOINTMENT_CHECKED + "(" + COLUMN_NAME_DATE_LAST_CHECKED + " DATE);";
db.execSQL(createAppointmentChecked);
} // End of method onCreate(...)
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PERSON);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_APPOINTMENT_CHECKED);
onCreate(db);
} // End of method onUpgrade(...)
// Method which adds the person object (received via parameter) in the database.
public void addAppointment(Person person)
{
myDB = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME_PERSONNAME, person.getName());
values.put(COLUMN_NAME_DATE, new SimpleDateFormat("yyyy-MM-dd").format(person.getDate().getTime()));
values.put(COLUMN_NAME_TIME, new SimpleDateFormat("HH:mm:ss").format(person.getTime().getTime()));
myDB.insert(TABLE_PERSON, null, values);
myDB.close();
} // End of method addAppointment(...)
// Method that extracts all the individuals in the database sorted by month and then day.
public ArrayList<Person> getListSortByAppointment()
{
ArrayList<Person> list = new ArrayList<Person>();
SQLiteDatabase rDB = getReadableDatabase();
Cursor cursor = rDB.query(TABLE_PERSON, new String[] {"*"}, null, null,null, null,
"strftime('%m', " + COLUMN_NAME_DATE + "), strftime('%d', " + COLUMN_NAME_DATE + ")"
+ " ASC", null);
if (cursor.moveToFirst())
{
do
{
String name = cursor.getString(1);
Calendar date = Calendar.getInstance();
Calendar time = Calendar.getInstance();
try
{
date.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(cursor.getString(4)));
}
catch (ParseException e)
{
e.printStackTrace();
}
Person person = new Person(name, time, date);
list.add(person);
} while (cursor.moveToNext());
}
rDB.close();
return list;
} // End of method getListSortByBirthday()
public ArrayList<Person> getPersonWidthAppointmentToday()
{
ArrayList<Person> list = new ArrayList<Person>();
SQLiteDatabase rDB = getReadableDatabase();
Cursor cursor = rDB.query(TABLE_PERSON, new String[] {"*"}, "strftime('%m-%d', " + COLUMN_NAME_DATE +
") = strftime('%m-%d', 'now')", null, null, null, null, null);
if (cursor.moveToFirst())
{
do
{
String name = cursor.getString(1);
Calendar date = Calendar.getInstance();
Calendar time = Calendar.getInstance();
try
{
date.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(cursor.getString(4)));
}
catch (ParseException e)
{
e.printStackTrace();
}
Person person = new Person(name, time, date);
list.add(person);
} while (cursor.moveToNext());
}
else
list = null;
rDB.close();
return list;
// End of method getPersonWidthAppointmentToday()
}
// The method updates the values ​​of the person (which comes in as parameter) in the database.
public boolean updatePerson(Person person)
{
myDB = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME_TIME, new SimpleDateFormat("HH:mm:ss").format(person.getDate().getTime()));
values.put(COLUMN_NAME_DATE, new SimpleDateFormat("yyyy-MM-dd").format(person.getDate().getTime()));
return myDB.update(TABLE_PERSON, values, COLUMN_NAME_PERSONNAME + " = ?", new String[]{String.valueOf(person.getName())}) > 0;
} // End of method updatePerson(...)
// This method deletes the person object from the database
public void deletePerson(Person person)
{
myDB = getWritableDatabase();
myDB.delete(TABLE_PERSON, COLUMN_NAME_PERSONNAME + " = ?", new String[]{String.valueOf(person.getName())});
myDB.close();
} // End of method deletePerson(...)
// Method which adds the date in the database.
public void addDateCheck(Calendar calendar)
{
myDB = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME_DATE_LAST_CHECKED, new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
myDB.insert(TABLE_APPOINTMENT_CHECKED, null, values);
myDB.close();
}
// End of method addAppointmentCheck(...)
// The method updates the values ​​of the date of the last check in the database.
public boolean updateDateCheck(Calendar calendar)
{
myDB = getWritableDatabase();
String checkDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
ContentValues values = new ContentValues();
values.put(COLUMN_NAME_DATE_LAST_CHECKED, checkDate);
return myDB.update(TABLE_APPOINTMENT_CHECKED, values, COLUMN_NAME_DATE_LAST_CHECKED + " = ?", new String[]{checkDate}) > 0;
} // End of method updateBirthdayCheck(...)
} // End of class DBHandler
你解決你的問題的價值?以下答案有幫助嗎? – Vamshi
不,我在下面的行中獲取錯誤SQLiteDatabase myDB = this.Oncreate(「SchedulerDB」,SQLiteDatabase.OPEN_READWRITE,null); on創建或openDatbase和dbObject中的錯誤 –
發佈您的日誌...此外,您的代碼是錯誤的寫入,因爲它就像SQLiteDatabase myDB = this.openOrCreateDatabase(「SchedulerDB」,SQLiteDatabase.OPEN_READWRITE,null); – Vamshi