如何在SQL数据库中保存用户Edittext输入?

时间:2016-08-04 14:37:49

标签: android sql

构建一个android TODO列表应用程序,用于在SQL数据库中保存用户输入,但无法弄清楚如何在OnClick事件中实现代码。

这里是我希望保存用户输入数据的SQL数据库代码

public class TaskContract {
public static final String DB_NAME = "com.aziflaj.todolist.db";
public static final int DB_VERSION = 1;

public class TaskEntry implements BaseColumns {
    public static final String TABLE = "tasks";
    public static final String COL_TASK_TITLE = "title";
    public static final String COL_TASK_DESCRIPTION = "Description";
    public static final String COL_TASK_DATE = "date";
    public static final String COL_TASK_TIME = "time";
}}

SQLDbHelper代码

public class TaskDbHelper extends SQLiteOpenHelper {

public TaskDbHelper(Context context) {
    super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TaskContract.TaskEntry.TABLE + " ( " +
            TaskContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            TaskContract.TaskEntry.COL_TASK_TITLE + " TEXT NOT NULL," +
            TaskContract.TaskEntry.COL_TASK_DATE + "TEXT NOT NULL," +
            TaskContract.TaskEntry.COL_TASK_DESCRIPTION + "TEXT NOT NULL," +
            TaskContract.TaskEntry.COL_TASK_TIME + "TEXT NOT NULL; )";

    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TaskContract.TaskEntry.TABLE);
    onCreate(db);
}}

活动布局代码

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".Dialog">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title_field"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:ems="20"
        android:hint="Task Title"
        android:textColorHint="@color/hint_color"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/description_field"
        android:layout_below="@+id/title_field"
        android:layout_marginTop="5dp"
        android:ems="20"
        android:hint="Task Description"
        android:textColorHint="@color/hint_color"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="date"
        android:hint="Date"
        android:textColorHint="@color/hint_color"
        android:ems="6"
        android:id="@+id/date_field"
        android:layout_below="@+id/description_field"
        android:layout_alignLeft="@+id/description_field"
        android:layout_alignStart="@+id/description_field"
        android:layout_marginTop="35dp"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="time"
        android:ems="6"
        android:hint="Time"
        android:textColorHint="@color/hint_color"
        android:layout_marginRight="20dp"
        android:id="@+id/time_field"
        android:layout_alignTop="@+id/date_field"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD"
        android:gravity="center|right"
        android:id="@+id/add"
        android:onClick="Add_task"
        android:textColor="@color/background"
        style="?android:attr/borderlessButtonStyle"
        android:layout_below="@+id/time_field"
        android:textSize="16dp"
        android:layout_marginBottom="10dp"
        android:layout_alignRight="@+id/time_field"
        android:layout_alignEnd="@+id/time_field"
        android:layout_marginTop="35dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CANCEL"
        android:onClick="Cancel"
        android:textSize="16dp"
        android:id="@+id/cancel"
        android:layout_marginBottom="10dp"
        style="?android:attr/borderlessButtonStyle"
        android:textColor="@color/background"
        android:layout_alignTop="@+id/add"
        android:layout_toLeftOf="@+id/add"
        android:layout_toStartOf="@+id/add"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

如果您还没有,那么您应该将值传递给扩展SQLiteOpenHelper的类,例如:(这有我当前的实现,因此相应地更改字段)

futureFuncs.iterator.buffered

从那里开始,您只需要一种方法可以在表格中添加内容,或者在您需要时删除。使用这样的东西:

public class UserDatabase extends SQLiteOpenHelper {
    public static final int database_version = 1;


    public UserDatabase(Context context) {
        super(context, TableInfo.DATABASE_NAME, null, database_version);
    }

    @Override
    public void onCreate(SQLiteDatabase sdb) {
            String CREATE_USER_TABLE_QUERY = "CREATE TABLE IF NOT EXISTS" + TableInfo.TALBE_NAME + "("+ TableInfo.USER_NAME + " TEXT," + TableInfo.USER_PASSWORD + " TEXT," + TableInfo.USER_AUTOLOGIN + " TEXT, " + TableInfo.SESSION_ID + " TEXT, " + TableInfo.SERVER_NAME + " TEXT );";
            sdb.execSQL(CREATE_USER_TABLE_QUERY);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
           //This can be used if you'd like, it's not necessary though
    }

在您需要获取代码的活动中,只需使用以下命令从编辑文本中获取文本:

public void putDataInUserTable(UserDatabase dop, String name, String pass, String autologin, String sessionId, String server) {
    SQLiteDatabase sql = dop.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(TableInfo.USER_NAME, name);
    cv.put(TableInfo.USER_PASSWORD, pass);
    cv.put(TableInfo.USER_AUTOLOGIN, autologin);
    cv.put(TableInfo.SESSION_ID, sessionId);
    cv.put(TableInfo.SERVER_NAME, server);
    long k = sql.insert(TableInfo.TALBE_NAME, null, cv);
}

然后使用您创建的类将其添加到表中,例如:

EditText item = (EditText) findViewById(R.id.item);
String itemText = item.getText().toString();