我想将textview中的数据添加到sqlite数据库中

时间:2014-02-20 08:48:25

标签: android sqlite

因为我是Android开发的新手。请告诉我如何将textview中的数据添加到数据库中。我已经创建了一个数据库处理程序类。但是无法知道在我的主要活动中写什么以便在sqlite中保存这些数据。 这是我的DBhelper类:

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

1 个答案:

答案 0 :(得分:0)

在主活动中创建TextView

TextView textview = (TextView) findViewByID(R.id.textview);    
String text = textview.getText().toString();

接下来打开数据库并插入如下所示的值

SQLiteDatabase myDB = this.openOrCreateDatabase("SchedulerDB", SQLiteDatabase.OPEN_READWRITE, null);
try {               
        SQLiteStatement stmt = dbObject.compileStatement("INSERT INTO Tablename (coulmnname) values (?)");              
        stmt.bindString(1, text);
        stmt.executeInsert();

} catch (Exception e) {
            e.printStackTrace();

} finally {             
            dbObject.close();           
}