java代码中的SQLite自动增量无法正常工作

时间:2015-06-04 13:02:16

标签: java android sqlite

我的项目组和我遇到了一个不寻常的错误。 我们正在开发一个应用程序并使用在JAVA代码中创建的本地数据库,我们正在使用Android Studio 2.1.1.1。

遗憾的是,当我们创建一个帐户时,它不会自动增加USER_ID,实际上它总是返回值" 0" (不带引号) 那么我们的Cursor有错误吗?我们创建SQLite数据库时是否有错误?据我所知,整数主键默认为自动增量。

private static final String TABLE_CREATE_USER =
            "CREATE TABLE " + USER_TABLE + " (" +
                    USER_ID + " INTEGER PRIMARY KEY," +
                    USERNAME + " VARCHAR(200) NULL, " +
                    NAME + " VARCHAR(200) NULL, " +
                    EMAIL + " VARCHAR(200) NULL, " +
                    PHONENUMBER + " VARCHAR(200) NULL, " +
                    BUSINESS + " VARCHAR(200) NULL, " +
                    BIO + " VARCHAR(200) NULL, " +
                    HASSTARTUP + " VARCHAR(200) NULL, " +
                    PASSWORD + " VARCHAR(45) NULL " +
                    ")";
if(!database.isOpen())
            open();

        ContentValues values = new ContentValues();

        String query = "SELECT * FROM user ";
        Cursor cursor = database.rawQuery(query, null);
        count = cursor.getCount();
        count++;

        values.put(DBOpenHelper.getUserId(), count);

        values.put(DBOpenHelper.getUsername(), user.get_AccountName());
        values.put(DBOpenHelper.getBio(), user.get_Bio());
        values.put(DBOpenHelper.getBusiness(), user.get_Businessname());
        values.put(DBOpenHelper.getEmail(), user.get_Email());
        values.put(DBOpenHelper.getPassword(), user.get_Password());
        values.put(DBOpenHelper.getPhonenumber(), user.get_Phonenumber());
        values.put(DBOpenHelper.getName(), user.get_Profilename());
        values.put(DBOpenHelper.getHasstartup(), user.get_hasStartup());

        database.insert(DBOpenHelper.getUserTable(), null, values);

        Log.i("Database", "CREATE SUCCES");

        if(database.isOpen())
            close();

    }

这就是我们现在所拥有的: 在这里我们创建表:

private static final String TABLE_CREATE_USER =
            "CREATE TABLE " + USER_TABLE + " (" +
                    USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    USERNAME + " VARCHAR(200) NULL, " +
                    NAME + " VARCHAR(200) NULL, " +
                    EMAIL + " VARCHAR(200) NULL, " +
                    PHONENUMBER + " VARCHAR(200) NULL, " +
                    BUSINESS + " VARCHAR(200) NULL, " +
                    BIO + " VARCHAR(200) NULL, " +
                    BRANCHE + " VARCHAR(200) NULL, " +
                    HASSTARTUP + " VARCHAR(200) NULL, " +
                    PASSWORD + " VARCHAR(45) NULL " +
                    ")";

将值放入用户:

    public void createUser(User user){

            if(!database.isOpen())
                open();

            ContentValues values = new ContentValues();

    //        String query = "SELECT * FROM user ";
    //        Cursor cursor = database.rawQuery(query, null);
    //        count = cursor.getCount();
    //        count++;

            values.put(DBOpenHelper.getUsername(), user.get_AccountName());
            values.put(DBOpenHelper.getBio(), user.get_Bio());
            values.put(DBOpenHelper.getBusiness(), user.get_Businessname());
            values.put(DBOpenHelper.getEmail(), user.get_Email());
            values.put(DBOpenHelper.getPassword(), user.get_Password());
            values.put(DBOpenHelper.getPhonenumber(), user.get_Phonenumber());
            values.put(DBOpenHelper.getName(), user.get_Profilename());
            values.put(DBOpenHelper.getHasstartup(), user.get_hasStartup());
            values.put(DBOpenHelper.getBranche(), user.get_branche());

            database.insert(DBOpenHelper.getUserTable(), null, values);

            Log.i("Database", "CREATE SUCCES");

            if(database.isOpen())
                close();

        }

Here we read/set the info from the create account fields:

public void onSignUpClick(View v)
    {
        if(v.getId() == R.id.BsignUpButton)
        {

            EditText email = (EditText)findViewById(R.id.TFEmail);
            EditText uname = (EditText)findViewById(R.id.TFUname);
            EditText companyName = (EditText)findViewById(R.id.TFCompanyName);
            EditText pass1 = (EditText)findViewById(R.id.TFPass1);
            EditText pass2 = (EditText)findViewById(R.id.TFPass2);
            EditText branche = (EditText)findViewById(R.id.TFCompanyBranche);

            String emailstr = email.getText().toString();
            String unamestr = uname.getText().toString();
            String companyNamestr = companyName.getText().toString();
            String pass1str = pass1.getText().toString();
            String pass2str = pass2.getText().toString();
            String branchestr = branche.getText().toString();

            final ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("Creating account");
            progressDialog.setMessage("Creating... Plz wait...");
            progressDialog.show();

            Runnable progressRunnable = new Runnable(){

                @Override
                public void run() {
                    progressDialog.cancel();
                }
            };

            Handler pdCanceller = new Handler();
            pdCanceller.postDelayed(progressRunnable, 3000);

            if(validatePassword(pass1str) && validateEmail(emailstr) && validatePassword(pass2str)){

                user.set_AccountName(unamestr);
                user.set_Profilename(" ");
                user.set_Businessname(companyNamestr);
                user.set_branche(branchestr);
                user.set_Password(pass1str);
                user.set_Email(emailstr);
                user.set_Bio(" ");
                user.set_Phonenumber(" ");
                user.set_hasStartup("yes");

                userDao.createUser(user);

                users = userDao.getUsers();

                Toast pass = Toast.makeText(this, "Account has been created", Toast.LENGTH_LONG);
                pass.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 10);
                pass.show();

                for (User u : users) {
                    String text = u.get_AccountName() + " " + u.get_id() + " " + u.get_branche();
                    Log.i("Database", text);
                }

//                logUserIn();
                Intent i = new Intent(this, Login.class);
                startActivity(i);
            }

            if(!pass1str.equals(pass2str))
            {
                pass2.setError("Passwords don't match");
                pass2.requestFocus();
            }

            if(emailstr.length() == 0 || unamestr.length() == 0 || pass1str.length() == 0 || pass2str.length() == 0 || companyNamestr.length() == 0 ){
                //popup msg
                Toast pass = Toast.makeText(SignUpBusiness.this, "Please fill in all the fields ", Toast.LENGTH_SHORT);
                pass.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 10);
                pass.show();
            }

            if(!validateEmail(emailstr)) {
                email.setError("Invalid Email");
                email.requestFocus();
            }
            if (!validatePassword(pass1str)) {
                pass1.setError("Invalid Password, use 10 or more characters");
                pass1.requestFocus();
            }
        }
    }

0 个答案:

没有答案