jeudi 5 novembre 2015

android.database.sqlite.SQLiteException:near"TABLEAccounts":syntax error(code 1):,while compiling: CREATE TABLEAccounts

public class DatabaseHelper extends SQLiteOpenHelper {

public static String dataBaseName="Login.db";

private static final int dataBaseVersion=1;

private static final String tableName="Accounts";
private static String Key_Id="id";
private static String Key_FirstName="firstname";
private static String Key_LastName="lastname";
private static String Key_Password="password";
private static String Key_Mobile="mobile";
private static String Key_Email="email";

public static String tag = "tag";

private static final String createTableAccounts="CREATE TABLE"+tableName+"("+Key_Id+"INTEGER PRIMARY KEY AUTOINCREMENT,"+Key_FirstName+"TEXT,"+Key_LastName+"TEXT,"+Key_Password+"TEXT,"+Key_Mobile+"TEXT,"+Key_Email+"TEXT);";

public  DatabaseHelper(Context context)
{
    super(context,dataBaseName,null,dataBaseVersion);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(createTableAccounts);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS"+createTableAccounts);
    onCreate(db);

}
public long addAccountDetials(AccountsModel accounts)
{
    SQLiteDatabase db= this.getWritableDatabase();

    ContentValues values=new ContentValues();
    values.put(Key_FirstName,accounts.firstname);
    values.put(Key_LastName,accounts.lastname);
    values.put(Key_Password,accounts.password);
    values.put(Key_Mobile,accounts.mobile);
    values.put(Key_Email, accounts.email);

    long insert =db.insert(tableName,null,values);
    return insert;
}
public int updateEntry(AccountsModel accounts)
{
    SQLiteDatabase db= this.getWritableDatabase();

    ContentValues values=new ContentValues();
    values.put(Key_FirstName,accounts.firstname);
    values.put(Key_LastName,accounts.lastname);
    values.put(Key_Password,accounts.password);
    values.put(Key_Mobile,accounts.mobile);
    values.put(Key_Email, accounts.email);

    return db.update(tableName,values,Key_Id+"=?",new String[]{String.valueOf(accounts.id)});
}
public void deleteEntry(long id)
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(tableName, Key_Id + " = ?",new String[] { String.valueOf(id) });
}}

MainActivity

public class MainActivity extends Activity implements OnClickListener{

private String firstName;
private String lastName;
private String mobile;
private String password;
private String email;

private EditText edtSignupFirstName;
private EditText edtSignupLastName;
private EditText edtSignupMobile;
private EditText edtSignupPassword;
private EditText edtSignupEmail;
private EditText edtId;

private Button btnSignupRegister;
private Button btnDelete;

DatabaseHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = new DatabaseHelper(getApplicationContext());

    edtSignupFirstName=(EditText)findViewById(R.id.edt_signup_first_name);
    edtSignupLastName=(EditText)findViewById(R.id.edt_signup_last_name);
    edtSignupMobile=(EditText)findViewById(R.id.edt_signup_mobile);
    edtSignupPassword=(EditText)findViewById(R.id.edt_signup_password);
    edtSignupEmail=(EditText)findViewById(R.id.edt_signup_email);
    edtId=(EditText)findViewById(R.id.edt_id);

    btnSignupRegister=(Button)findViewById(R.id.btn_signup_register);
    btnDelete=(Button)findViewById(R.id.btn_delete);

    btnSignupRegister.setOnClickListener(this);
    btnDelete.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    if (v==findViewById(R.id.btn_signup_register))
    {
        AccountsModel accounts=new AccountsModel();
        accounts.firstname=edtSignupFirstName.getText().toString();
        accounts.lastname=edtSignupLastName.getText().toString();
        accounts.password=edtSignupPassword.getText().toString();
        accounts.mobile=edtSignupMobile.getText().toString();
        accounts.email=edtSignupEmail.getText().toString();
        db.addAccountDetials(accounts);

        Toast.makeText(MainActivity.this, "DB ADDED", Toast.LENGTH_SHORT).show();

    }
    if (v==findViewById(R.id.btn_delete))
    {
        String account_id=edtId.getText().toString();
        db.deleteEntry(Integer.parseInt(account_id));
    }

}}

AuthenticationActivity

public class AuthenticationActivity extends AppCompatActivity {

private EditText edtMobile,edtPassword;
private Button btnLogin;
private Button btnSignup;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_authentication);

    edtMobile = (EditText) findViewById(R.id.edt_mobile);
    edtPassword = (EditText) findViewById(R.id.edt_password);

    btnLogin = (Button) findViewById(R.id.btn_login);
    btnSignup = (Button) findViewById(R.id.btn_signup);

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(AuthenticationActivity.this, "LOGED IN", Toast.LENGTH_SHORT).show();

            String authenticationActivtyMobile = edtMobile.getText().toString();
            String authenticationActivtyPassword = edtPassword.getText().toString();

            //Mobile
            if(authenticationActivtyMobile.length() == 10){

            }else{
                Toast.makeText(AuthenticationActivity.this, "Enter Only 10 Digit Number", Toast.LENGTH_SHORT).show();
                return;
            }
            String phone = String.valueOf(authenticationActivtyMobile);
            char c  = phone.charAt(0);
            if (c == '8' || c == '9' ||c =='7'){

            }else if( c == '0' ||c == '1' ||c == '2' ||c == '3' ||c == '4' ||c == '5' ||c == '6')
            {
                Toast.makeText(AuthenticationActivity.this, "Number Must Begin with 9 8 7",Toast.LENGTH_SHORT).show();
                return;
            }
            //Password
            if(authenticationActivtyPassword.length() <4){
                Toast.makeText(AuthenticationActivity.this, "Password Must Have Minimum 4 Character", Toast.LENGTH_SHORT).show();
                return;
            }else if(authenticationActivtyPassword.length()>=15){
                Toast.makeText(AuthenticationActivity.this, "Password Can Have Maximum 8 Character", Toast.LENGTH_SHORT).show();
                return;
            }



        }
    });
    btnSignup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(AuthenticationActivity.this, "Opening MainActivity Page", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(AuthenticationActivity.this, MainActivity.class);
            startActivity(intent);
        }
    });
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

here by clicking signup button it moves to Signup Activity where registeration take place,after entering detials while clicking Register button it throws "unfortunately stopped"in my emulator.

Aucun commentaire:

Enregistrer un commentaire