在SQLite数据库中创建表无法正常工作

时间:2019-06-03 10:39:05

标签: android android-sqlite oncreate

我正在制作一个Android应用程序,并希望将数据发送到SQLite数据库。我应该如何编码?

这是我创建表格的方式

public class DBHelper extends SQLiteOpenHelper {

    private final static String DATABASE_NAME = "UserInfo.db";

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

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        String CREATE_TABLE =
                "CREATE TABLE " + UserProfile.Users.TABLE_NAME + " (" +
                        UserProfile.Users._ID + " INTEGER PRIMARY KEY," +
                        UserProfile.Users.COLUMN_USERNAME + " TEXT," +
                        UserProfile.Users.COLUMN_DOB + " TEXT," +
                        UserProfile.Users.COLUMN_GENDER + " TEXT," +
                        UserProfile.Users.COLUMN_PASSWORD + " TEXT )";

        sqLiteDatabase.execSQL(CREATE_TABLE);
    }

这是此代码中的onUpgrade方法

 @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

这是我将addInfo方法添加到此类的方法

public long addInfo(String username, String password){

        SQLiteDatabase sqLiteDatabase = getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(UserProfile.Users.COLUMN_USERNAME, username);
        contentValues.put(UserProfile.Users.COLUMN_PASSWORD, password);

        long rowId = sqLiteDatabase.insert(UserProfile.Users.TABLE_NAME, null, contentValues);

        return rowId;
    }

    public int updateInfo(String userId, String userName, String password, String dob, String gender){

        SQLiteDatabase sqLiteDatabase = getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(UserProfile.Users.COLUMN_USERNAME, userName);
        values.put(UserProfile.Users.COLUMN_PASSWORD, password);
        values.put(UserProfile.Users.COLUMN_GENDER, gender);
        values.put(UserProfile.Users.COLUMN_DOB, dob);

        String selection = UserProfile.Users._ID + " = ?";
        String args[] = {userId};

        int count = sqLiteDatabase.update(UserProfile.Users.TABLE_NAME, values, selection, args);

        return count;
    }

下面是readAllInfo的数组列表

public ArrayList readAllInfo(){

        SQLiteDatabase sqLiteDatabase = getReadableDatabase();

        String[] projection = {

                UserProfile.Users._ID,
                UserProfile.Users.COLUMN_USERNAME,
                UserProfile.Users.COLUMN_DOB,
                UserProfile.Users.COLUMN_GENDER,
                UserProfile.Users.COLUMN_PASSWORD
        };

下面用于排序的代码

String sortOrder = UserProfile.Users._ID + " DESC";

        Cursor cursor = sqLiteDatabase.query(
                UserProfile.Users.TABLE_NAME,
                projection,
                null,
                null,
                null,
                null,
                sortOrder
        );

        ArrayList<User> list = new ArrayList<>();

        if (cursor.getCount() > 0){

            while(cursor.moveToNext()){

                User newUser = new User();

                int id = cursor.getInt(cursor.getColumnIndexOrThrow(UserProfile.Users._ID));
                String user = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_USERNAME));
                String date = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_DOB));
                String gen = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_GENDER));
                String pass = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_PASSWORD));

                newUser.setUserId(id+"");
                newUser.setUserName(user);
                newUser.setDateOfBirth(date);
                newUser.setGender(gen);
                newUser.setPassword(pass);

                list.add(newUser);
            }
        }

        return list;
    }

我用于readAllInfo方法的数组列表是这个

public ArrayList readAllInfo(String userId, String userName){

        String selection;
        String[] args = {""};

        if(userId == null){

            selection = UserProfile.Users.COLUMN_USERNAME + " LIKE ?";
            args[0] = userName;
        }
        else
        {
            selection = UserProfile.Users._ID + " = ?";
            args[0] = userId;
        }

        SQLiteDatabase sqLiteDatabase = getReadableDatabase();

        String[] projection = {

                UserProfile.Users._ID,
                UserProfile.Users.COLUMN_USERNAME,
                UserProfile.Users.COLUMN_DOB,
                UserProfile.Users.COLUMN_GENDER,
                UserProfile.Users.COLUMN_PASSWORD
        };



        String sortOrder = UserProfile.Users._ID + " DESC";

        Cursor cursor = sqLiteDatabase.query(
                UserProfile.Users.TABLE_NAME,
                projection,
                selection,
                args,
                null,
                null,
                sortOrder
        );

        ArrayList<User> list = new ArrayList<>();

        if (cursor.getCount() > 0){

            while(cursor.moveToNext()){

                User newUser = new User();

                int id = cursor.getInt(cursor.getColumnIndexOrThrow(UserProfile.Users._ID));
                String user = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_USERNAME));
                String date = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_DOB));
                String gen = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_GENDER));
                String pass = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_PASSWORD));

                newUser.setUserId(id+"");
                newUser.setUserName(user);
                newUser.setDateOfBirth(date);
                newUser.setGender(gen);
                newUser.setPassword(pass);

                list.add(newUser);
            }
        }

        return list;
    }

    public int deleteInfo(String username){

        SQLiteDatabase sqLiteDatabase = getReadableDatabase();

        String selection = UserProfile.Users._ID + " = ?";
        String[] args = {username};

        int deletedRows = sqLiteDatabase.delete(UserProfile.Users.TABLE_NAME, selection, args);

        return deletedRows;
    }
}

1 个答案:

答案 0 :(得分:2)

运行您的代码,它可以正常工作,因为这样的问题是您使用什么来确定它不起作用。

  • 存在一个异常,即 deleteInfo 方法使用 userName 作为传递的参数名称,但根据 userId 删除。

作为示例,在您的活动中使用您的代码以及以下内容:-

public class MainActivity extends AppCompatActivity {

    DBHelper mDBHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDBHelper = new DBHelper(this);
        mDBHelper.addInfo("UserFred","passwordforfred");
        mDBHelper.addInfo("UserMary","passwordformary");
        mDBHelper.updateInfo("1","UserWasFred","fred","2019-01-01","x");
        mDBHelper.updateInfo("2","UserEasMary","Mary","2019-02-01","Y");
        ArrayList<User> allusers = mDBHelper.readAllInfo();
        for (User u: allusers) {
            Log.d("USERINFO1","User is " + u.getUserName() + " DOB is " + u.getDateOfBirth());
        }
        ArrayList<User> auser = mDBHelper.readAllInfo("1","UserWasFred");
        for (User u: auser) {
            Log.d("USERINFO2","User is " + u.getUserName() + " DOB is " + u.getDateOfBirth());
        }
        mDBHelper.addInfo("UselessUser","useless");
        allusers = mDBHelper.readAllInfo();
        for (User u: allusers) {
            Log.d("USERINFO3","User is " + u.getUserName() + " DOB is " + u.getDateOfBirth());
        }
        mDBHelper.deleteInfo("Uselessuser");
        allusers = mDBHelper.readAllInfo();
        for (User u: allusers) {
            Log.d("USERINFO4","User is " + u.getUserName() + " DOB is " + u.getDateOfBirth());
        }
        mDBHelper.deleteInfo("3"); //<<<<<<<<<<< Works but userId passed
        allusers = mDBHelper.readAllInfo();
        for (User u: allusers) {
            Log.d("USERINFO5","User is " + u.getUserName() + " DOB is " + u.getDateOfBirth());
        }
    }
}

日志的输出显示上述所有操作均符合预期:-

06-04 08:04:53.874 D/USERINFO1: User is UserEasMary DOB is 2019-02-01
06-04 08:04:53.874 D/USERINFO1: User is UserWasFred DOB is 2019-01-01
06-04 08:04:53.875 D/USERINFO2: User is UserWasFred DOB is 2019-01-01
06-04 08:04:53.880 D/USERINFO3: User is UselessUser DOB is null
06-04 08:04:53.880 D/USERINFO3: User is UserEasMary DOB is 2019-02-01
06-04 08:04:53.880 D/USERINFO3: User is UserWasFred DOB is 2019-01-01
06-04 08:04:53.880 D/USERINFO4: User is UselessUser DOB is null
06-04 08:04:53.880 D/USERINFO4: User is UserEasMary DOB is 2019-02-01
06-04 08:04:53.880 D/USERINFO4: User is UserWasFred DOB is 2019-01-01
06-04 08:04:53.887 D/USERINFO5: User is UserEasMary DOB is 2019-02-01
06-04 08:04:53.887 D/USERINFO5: User is UserWasFred DOB is 2019-01-01