SQLite表已删除,现在不创建新表

时间:2019-04-07 11:27:44

标签: android sqlite

我用以下语句删除了我的旧表:         db.execSQL(“如果存在则删除表” + TABLE_REGISTER); 用方法addUser();

编写

我认为表格已删除。但是现在当我注册新用户时。用户注册失败。我认为没有创建新表。我还从addUser()方法中删除了以上语句。

public class DBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "agent.db";
    private static final int DATABASE_VERSION = 1;

    private static final String KEY_ID = "[ID]";
    private static final String KEY_FNAME = "[FirstName]";
    private static final String KEY_LNAME = "[LastName]";
    private static final String KEY_REGDATE = "[RegistrationDate]";
    private static final String KEY_USERID = "[UserID]";
    private static final String KEY_PASSWORD = "[Password]";

    private static final String TABLE_REGISTER = "tbRegister";
    private static final String CREATE_TABLE_REGISTER = "CREATE TABLE " + TABLE_REGISTER + "("
                                                        + KEY_ID + "INTEGER PRIMARY KEY,"
                                                        + KEY_FNAME + "TEXT NOT NULL,"
                                                        + KEY_LNAME + "TEXT NOT NULL,"
                                                        + KEY_REGDATE + "TEXT NOT NULL,"
                                                        + KEY_USERID + "TEXT NOT NULL,"
                                                        + KEY_PASSWORD + "TEXT NOT NULL )";


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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_REGISTER);

    }

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

    }

    public long addUser(User user) {

        SQLiteDatabase db = getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID, user.getId());
        values.put(KEY_FNAME, user.getfName());
        values.put(KEY_LNAME, user.getlName());
        values.put(KEY_REGDATE, user.getRegDate());
        values.put(KEY_USERID, user.getUserID());
        values.put(KEY_PASSWORD, user.getPassword());

        return db.insert(TABLE_REGISTER, null, values);
    }

    public List<User> getAllUsers() {
        List<User> userList = new ArrayList<>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * from " + TABLE_REGISTER, null);

        if (cursor.moveToFirst()){
            do {
                int id = cursor.getInt(0);
                String fName = cursor.getString(1);
                String lName = cursor.getString(2);
                String regDate = cursor.getString(3);
                String userID = cursor.getString(4);
                String password = cursor.getString(5);

                User user = new User(id, fName, lName, regDate, userID, password);
                userList.add(user);

            }while (cursor.moveToNext());
            cursor.close();
        }

        return userList;

    }
}

04-07 17:05:40.344 28263-28263/? E/Zygote: v2
04-07 17:05:40.344 28263-28263/? E/Zygote: accessInfo : 0
04-07 17:06:09.354 28263-28263/pk.edu.vu.agentpawnbroker E/SQLiteLog: (1) table tbRegister has no column named FirstName
04-07 17:06:09.364 28263-28263/pk.edu.vu.agentpawnbroker E/SQLiteDatabase: Error inserting [Password]=ppp [FirstName]=Majid [UserID]=munir64 [RegistrationDate]=07042019 [LastName]=Munir
    android.database.sqlite.SQLiteException: table tbRegister has no column named FirstName (code 1): , while compiling: INSERT INTO tbRegister([Password],[FirstName],[UserID],[RegistrationDate],[LastName]) VALUES (?,?,?,?,?)
    #################################################################
    Error Code : 1 (SQLITE_ERROR)
    Caused By : SQL(query) error or missing database.
        (table tbRegister has no column named FirstName (code 1): , while compiling: INSERT INTO tbRegister([Password],[FirstName],[UserID],[RegistrationDate],[LastName]) VALUES (?,?,?,?,?))
    #################################################################
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)

1 个答案:

答案 0 :(得分:0)

每次运行应用程序时,onCreate()中的代码不会执行。
所以不要指望该语句:

db.execSQL(CREATE_TABLE_REGISTER);

下次运行应用程序时将重新创建表。
您必须从用于测试应用程序的设备上卸载该应用程序。
这样,数据库将被删除,当您重新运行应用程序时,它将重新创建数据库,并且onCreate()中的代码将被执行以创建表。
仅当数据库不存在时才调用方法onCreate(),或者您可以在onUpgrade()内部直接说它。
还将create语句更改为此:

private static final String CREATE_TABLE_REGISTER = "CREATE TABLE " + TABLE_REGISTER + "("
    + KEY_ID + " INTEGER PRIMARY KEY,"
    + KEY_FNAME + " TEXT NOT NULL,"
    + KEY_LNAME + " TEXT NOT NULL,"
    + KEY_REGDATE + " TEXT NOT NULL,"
    + KEY_USERID + " TEXT NOT NULL,"
    + KEY_PASSWORD + " TEXT NOT NULL)";

您错过了列名称及其数据类型之间的几个空格。
编辑
addUser()删除此行:

values.put(KEY_ID, user.getId());

KEY_ID被定义为PRIMARY KEY,对于SQLite来说,它也将是AUTOINCREMENT,并且您不能为其提供值。