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

标签 android sqlite

因为我是 Android 开发的新手。请告诉我如何将数据从 textview 添加到数据库。我已经创建了一个数据库处理程序类。但无法知道在我的主要 Activity 中写什么以便将数据保存在 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

最佳答案

在主 Activity 中创建 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();           
}

关于android - 我想将 textview 中的数据添加到 sqlite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21902552/

相关文章:

android - 捕获 QProcess (ls) 到 ListView

android - Spinner 默认主题到 EditText android

triggers - sqlite3 触发器将十六进制文本转换为等效的二进制 blob

android - 显示教程

android Build.GetSerial() 抛出异常

android - 如何从 ListView 中提取对象 - getItemAtPosition 不起作用

java - Glassfish SQLite 数据源

ios - 在 Xcode iOS SDK 中读取 SQLite

c# - SQLite + SpatiaLite 问题

sql - 如何在两个 SELECT 集之间进行订阅?