无法访问活动中的数据库

时间:2019-06-02 11:04:02

标签: android sqlite

我正在尝试编写将注册用户并将其添加到数据库的代码。在此之前,它将使用“数据库帮助程序”类中编写的函数检查电子邮件和用户名是否存在。问题是每次我尝试访问数据库时app崩溃

这是数据库帮助程序类:

public class DatabaseHelper extends SQLiteOpenHelper {
    // database name
    public  static final String DATABASENAME= "Register.db";
    //column names
    public  static final String username= "username";
    public  static final String email= "email";
    public  static final String password= "password";
    public  static final String wins= "wins";
    public  static final String losses= "losses";
    public  static  final String points= "points";
    public  static  final String alltimewinnings= "alltime";
    public  static  final String ratio= "ratio";
    //table create statement
    private static final String CREATETABLE= "CREATE TABLE "+ "(" +username + " TEXT PRIMARY KEY," + email + " TEXT,"
            + password+ " Text" + wins + " TEXT," + losses+ "Text"+points+ "Text"+alltimewinnings+"Text"+ratio+"Text"+")";
    public DatabaseHelper(Context context) {
        super(context,DATABASENAME, null, 1);
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + CREATETABLE);
 }
    public boolean AddUser(User user) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("username", user.GetName());
        contentValues.put("email", user.GetEmail());
        contentValues.put("password", user.GetPassword());
        contentValues.put("wins", 0);
        contentValues.put("losses", 0);
        contentValues.put("points", 1000);
        contentValues.put("alltime",0);
        contentValues.put("ratio",0);
        long ins = db.insert(CREATETABLE, null, contentValues);
        if (ins == 1) return false;
        else return true;
    }
 public Boolean CheckMail (String emailaddress) //checks if email exists already
    {
        SQLiteDatabase db= this.getReadableDatabase();
        String selectQuery = "SELECT  * FROM " + CREATETABLE + " WHERE "
                + email + " = " + emailaddress;
        //Log.e(, selectQuery);// SHOWS IN ERROR FOR DEBUGGING
        Cursor cursor= db.rawQuery(selectQuery,null);
        if(cursor.getCount()>0) return false;
        else return true;
    }
    public Boolean CheckUserName (String name) //checks if username exists already
    {
        SQLiteDatabase db= this.getReadableDatabase();
        String selectQuery = "SELECT  * FROM " + CREATETABLE + " WHERE "
                + username + " = " + name;
        //Log.e(, selectQuery);// SHOWS IN ERROR FOR DEBUGGING
        Cursor cursor= db.rawQuery(selectQuery,null);
        if(cursor.getCount()>0) return false;
        else return true;
    }

这是寄存器类:

 DatabaseHelper db= new DatabaseHelper(this);// database of users
    Button Register;//  button of registration
    EditText password;// the user's password
    EditText email;//the user's email
    EditText userid;// the user's name
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
         Register= (Button) findViewById(R.id.register);
         email= (EditText) findViewById(R.id.EMAIL);
         password= (EditText) findViewById(R.id.PASSWORD);
         userid= (EditText) findViewById(R.id.username);
        Register.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {

                String userId = userid.getText().toString();
                String Password = password.getText().toString();
                String Email = email.getText().toString();
                User user= new User(userId,Password,Email);
                if(user.equals("")|| Password.equals("")|| Email.equals(""))//IF ANY IF FIELDS IS NULL
                { // say the status of the registration
                    Toast.makeText(getApplicationContext(), "Fields are empty",Toast.LENGTH_SHORT).show();
                }
                else
                { Boolean checkmail= db.CheckMail(user.GetEmail());
                    Boolean checkusername= db.CheckUserName(user.GetName());
                    if(checkmail==false && checkusername==true) {
                        // say the status of the registration
                        Toast.makeText(getApplicationContext(), "Registration failed. Email already exists", Toast.LENGTH_SHORT).show();
                    }
                    else if(checkmail==true && checkusername==false) {
                        // say the status of the registration
                        Toast.makeText(getApplicationContext(), "Registration failed. Username already exists", Toast.LENGTH_SHORT).show();
                    }
                    else if(checkmail==false && checkusername==false) {
                        // say the status of the registration
                        Toast.makeText(getApplicationContext(), "Registration failed. Email and Username already exists", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Boolean insert = db.AddUser(user);
                            if (insert == true)
                            { //save the user's name in  SharedPreferences before registration for access in other activities
                                SharedPreferences ShareName= getSharedPreferences("data",MODE_PRIVATE);
                                SharedPreferences.Editor editor= ShareName.edit();
                                editor.putString("username",userId);
                                editor.commit();
                                // say the status of the registration
                                Toast.makeText(getApplicationContext(), "Registration is complete", Toast.LENGTH_SHORT).show();
                                //move to the next page
                                Intent intent = new Intent(Register.this, HomePage.class);
                                startActivity(intent);
                            }
                        // say the status of the registration
                        Toast.makeText(getApplicationContext(), "Error occurred. Please try again later", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

您的CREATE TABLE语句由于缺少逗号和空格而出现了多个错误。
更改为此:

private static final String CREATETABLE= "CREATE TABLE "+ "(" + 
    username + " TEXT PRIMARY KEY," + email + " TEXT," + 
    password + " TEXT," + wins + " TEXT," + losses + " TEXT," + points + " TEXT," + 
    alltimewinnings + " TEXT," + ratio + " TEXT)";

之后,从设备上卸载应用程序并重新运行。