无法在SQl数据中保存Android用户输入

时间:2016-08-06 19:11:17

标签: android sql

我是Android的新手,并尝试构建一个使用SQL数据库保存Edittext用户输入的待办事项列表Android应用程序,数据库有1个表和4列(日期,描述,标题,时间)但是当我尝试保存它时使用onClick事件会出现以下错误。

08-07 00:05:52.015 11463-11463/com.naive.LISTY E/SQLiteLog: (1) table tasks has no column named date
08-07 00:05:52.026 11463-11463/com.naive.LISTY E/SQLiteDatabase: Error inserting date=12/12/12 Description=hello world title=hello time=12:30
                                                                 android.database.sqlite.SQLiteException: table tasks has no column named date (code 1): , while compiling: INSERT INTO tasks(date,Description,title,time) VALUES (?,?,?,?)
                                                                     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                                     at listy.naive.com.listy.Dialog$override.Add_task(Dialog.java:64)
                                                                     at listy.naive.com.listy.Dialog$override.access$dispatch(Dialog.java)
                                                                     at listy.naive.com.listy.Dialog.Add_task(Dialog.java:0)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                     at android.view.View.performClick(View.java:5198)
                                                                     at android.view.View$PerformClick.run(View.java:21147)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

SQLcontact代码

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(TaskDbHelper)

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);
}}

活动(Dialog.java)代码

public class Dialog extends AppCompatActivity {

private TaskDbHelper mHelper;
private ListView mTaskListView;
private ArrayAdapter<String> mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dialog);
    mHelper = new TaskDbHelper(this);

}



public void Cancel(View view) {

    finish();
}

public void Add_task(View view) {

    EditText mTitle = (EditText)findViewById(R.id.title_field);
    String Title = String.valueOf(mTitle.getText());

     EditText mTask = (EditText)findViewById(R.id.description_field);
     String Task = String.valueOf(mTask.getText());

    EditText mDate = (EditText) findViewById(R.id.date_field);
    String Date = String.valueOf(mDate.getText());

    EditText mTime = (EditText) findViewById(R.id.time_field);
    String Time = String.valueOf(mTime.getText());

    SQLiteDatabase db = mHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(TaskContract.TaskEntry.COL_TASK_TITLE, Title);
    values.put(TaskContract.TaskEntry.COL_TASK_DESCRIPTION, Task);
    values.put(TaskContract.TaskEntry.COL_TASK_DATE, Date);
    values.put(TaskContract.TaskEntry.COL_TASK_TIME, Time);

    db.insert(TaskContract.TaskEntry.TABLE, null, values);
    db.close();


}

删除 values.put(TaskContract.TaskEntry.COL_TASK_DATE,Date); 时,它开始为列时间和其他时间分别提供错误。

1 个答案:

答案 0 :(得分:1)

问题在于此

        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; )";

请检查“TEXT NOT NULL”的空格,你应该包含空格......它应该像“TEXT NOT NULL”,对于TaskContract.TaskEntry.COL_TASK_DESCRIPTION以及对于TaskContract.TaskEntry.COL_TASK_TIME ..并删除分号后不是;到底 并在您的测试设备上卸载并重新安装该应用程序。