重新启动应用程序后清空SQL lite数据库

时间:2018-10-03 04:50:52

标签: java android sqlite

重新启动应用程序后,我的数据库始终为空。

我做错了什么?

我一直都在清理数据库吗?

这是数据库类的代码。

import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;

    import java.sql.Date;
    import java.util.ArrayList;

    import galimski.igor.com.do_ing.Task;
    import galimski.igor.com.do_ing.TaskPriority;


    public class DatabaseHelper extends SQLiteOpenHelper {

        private static final String TAG = "SQLite";

        private static final int DATABASE_VERSION = 1;

        private static final String DATABASE_NAME = "TaskDatabase";

        private static final String TABLE_TASK = "Task";

        private static final String COLUMN_TASK_ID ="Id";
        private static final String COLUMN_TASK_SHORT ="ShortDescription";
        private static final String COLUMN_TASK_FULL = "FullDescription";

        private static final String COLUMN_TASK_CREATIONDATE = "CreatedDate";
        private static final String COLUMN_TASK_COMPLETIONDATE = "CompletionDate";

        private static final String COLUMN_TASK_PRIORITY = "Priority";

        private static final String COLUMN_TASK_SHOWN= "Shown";

        public DatabaseHelper(Context context)  {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.i(TAG, "MyDatabaseHelper.onCreate ... ");

            //        String script = "CREATE TABLE " + TABLE_NOTE + "("
            //                + COLUMN_NOTE_ID + " INTEGER PRIMARY KEY," + COLUMN_NOTE_TITLE + " TEXT,"
            //                + COLUMN_NOTE_CONTENT + " TEXT" + ")";
            //String script = "CREATE TABLE `Task` ( `Id` INTEGER NOT NULL, `ShortDescription` TEXT NOT NULL, `FullDescription` TEXT, `CreatedDate` TEXT NOT NULL, `CompletionDate` TEXT NOT NULL, `Priority` TEXT NOT NULL, PRIMARY KEY(`Id`) )";

            String script = "CREATE TABLE " + TABLE_TASK + "("
                            + COLUMN_TASK_ID + " INTEGER PRIMARY KEY,"
                            + COLUMN_TASK_SHORT + " TEXT,"
                            + COLUMN_TASK_CREATIONDATE + " TEXT,"
                            + COLUMN_TASK_COMPLETIONDATE + " TEXT,"
                            + COLUMN_TASK_PRIORITY + " TEXT,"
                            + COLUMN_TASK_SHOWN + " TEXT" + ")";

            db.execSQL(script);
        }


        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.i(TAG, "MyDatabaseHelper.onUpgrade ... ");

            if (oldVersion != newVersion) {
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASK);
                onCreate(db);
            }

        }

        // Called when the database connection is being configured.
        // Configure database settings for things like foreign key support, write-ahead logging, etc.
        @Override
        public void onConfigure(SQLiteDatabase db) {
            super.onConfigure(db);
            db.setForeignKeyConstraintsEnabled(true);
        }

        public void AddTask(Task task) {
            Log.i(TAG, "MyDatabaseHelper.addTask ... ");

            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();

            values.put(COLUMN_TASK_SHORT, task.GetShortDescription());
            values.put(COLUMN_TASK_FULL, task.GetFullDescription());

            values.put(COLUMN_TASK_CREATIONDATE, task.GetCreatedDate().toString());
            values.put(COLUMN_TASK_COMPLETIONDATE, task.GetCompletionDate().toString());

            values.put(COLUMN_TASK_PRIORITY, task.GetCompletionDate().toString());

            db.insert(TABLE_TASK, null, values);

            db.close();
        }
    }

2 个答案:

答案 0 :(得分:0)

请在真实的android设备上测试您的应用程序,并检查是否仍然存在相同的问题,并且仅在重新启动该应用程序后才从android studio重新运行,因为每次您重新运行android studio都会重新安装该应用程序在空数据库中。

1)在设备上运行应用程序 2)将数据添加到数据库中 3)关闭应用程序 4)从最近的应用程序中删除它。 5)从菜单而不是从android studio重新运行它。

答案 1 :(得分:0)

您遇到的问题不是数据库正在被删除或清空。这是因为在您尝试使用列 FullDescription (根据values.put(COLUMN_TASK_FULL, task.GetFullDescription());进行插入时,插入操作不会插入任何数据)。 但是,该列在表中不存在(根据您显示的创建表SQL)。

您需要注释掉或删除values.put(COLUMN_TASK_FULL, task.GetFullDescription());

或者如果您需要该列,请更改

String script = "CREATE TABLE " + TABLE_TASK + "("
                                + COLUMN_TASK_ID + " INTEGER PRIMARY KEY,"
                                + COLUMN_TASK_SHORT + " TEXT,"
                                + COLUMN_TASK_CREATIONDATE + " TEXT,"
                                + COLUMN_TASK_COMPLETIONDATE + " TEXT,"
                                + COLUMN_TASK_PRIORITY + " TEXT,"
                                + COLUMN_TASK_SHOWN + " TEXT" + ")";

String script = "CREATE TABLE " + TABLE_TASK + "("
                            + COLUMN_TASK_ID + " INTEGER PRIMARY KEY,"
                            + COLUMN_TASK_SHORT + " TEXT,"
                            + COLUMN_TASK_FULL + " TEXT," 
                            + COLUMN_TASK_CREATIONDATE + " TEXT,"
                            + COLUMN_TASK_COMPLETIONDATE + " TEXT,"
                            + COLUMN_TASK_PRIORITY + " TEXT,"
                            + COLUMN_TASK_SHOWN + " TEXT" + ")";

如果通过添加列来更改表的定义,则仅在执行以下操作之一后才需要重新运行应用程序:-

  1. 删除应用程序的数据
  2. 卸载应用
  3. private static final int DATABASE_VERSION = 1;更改为private static final int DATABASE_VERSION = 2;

这是因为 onCreate 方法仅在创建数据库时运行一次 1 2删除数据库 3 通过 onUpgrade 方法

调用 onCreate 方法