편집 기록

편집 기록
  • 프로필 알 수 없는 사용자님의 편집
    날짜2016.04.03

    sqlite 이용해서 로그인폼 만드는 중에 자꾸 테이블이 재생성되는 것같아요..(완전초보입니당)


    .

  • 프로필 허대영(소프트웨어융합대학)님의 편집
    날짜2016.04.02

    sqlite 이용해서 로그인폼 만드는 중에 자꾸 테이블이 재생성되는 것같아요..(완전초보입니당)


    안드로이드 스튜디오로 앱을 실행했을 때 로그인화면에서 회원가입 클릭한 후 회원가입화면까지는 잘 뜹니다. 그런데 회원가입을 하고 환영인사 토스트문장도 나오는데 로그인 화면으로 돌아가서 가입에 입력했던 아이디와 비번을 입력하면 잘못된정보라고 자꾸 뜹니다.. 아무래도 DB가 재생성 되는 것 같은데 어떻게 해결해야하나요..? 제가 정말 초보라 자세한 설명 또는 수정 부탁드립니다.... 도와주세요 고수님들...ㅠㅠ

    이게 sqlite이용한 Info.java 데이터 파일이구요.

    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.widget.Toast;
    
    public class Info extends ActionBarActivity {
        SQLiteDatabase database;
        CustomerDatabaseHelper databaseHelper;
        String tableName = "PRODUCT";
        String databaseName = "memberJoin";
        String Cnum;
        String Cpass;
        String Cname;
        String Cmajor;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            try
    
            {
                if (database == null) {
                    //  database = openOrCreateDatabase(databaseName, Context.MODE_PRIVATE, null);
                    databaseHelper = new CustomerDatabaseHelper(getApplicationContext(), databaseName, null, 1);
                    database = databaseHelper.getWritableDatabase();
                    Toast.makeText(getApplication(), "DB :" + databaseName + "이 생성되었습니다.", Toast.LENGTH_SHORT).show();
                } else if (database != null) {
                    Toast.makeText(getApplication(), "이미 디비열렸음", Toast.LENGTH_SHORT).show();
                }
    
            } catch (
                    Exception e
                    )
    
            {
                e.printStackTrace();
            }
            try {
                if (database != null) {
                    database.execSQL("CREATE TABLE if not exists " + tableName + "(" +
                            "_id integer PRIMARY KEY autoincrement," +
                            "name text not null," +
                            "pass text not null," +
                            "write text not null," +
                            "num text not null," +
                            "major text not null" +
                            ")");
                    Toast.makeText(getApplication(), "Table :" + tableName + "이 생성되었습니다.", Toast.LENGTH_SHORT).show();
    
                }
            } catch (Exception e) {
    
                e.printStackTrace();
            }
    
        }
        class CustomerDatabaseHelper extends SQLiteOpenHelper {
            CustomerDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
                super(context, name, factory, version);
            }
    
            @Override
            public void onOpen(SQLiteDatabase database) {
                super.onOpen(database);
            }
    
            @Override
            public void onCreate(SQLiteDatabase database) {
            }
    
            @Override
            public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
    
            }
        }
    
    }
    

    그다음 ..login.java파일입니다.

    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    
    public class Login extends Info {
    
    
    
        EditText idText;
        EditText passText;
        String Id;
        String Pass;
    
    
    
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            if (getIntent().getExtras() == null) {
                startActivity(new Intent(this, StartView.class));
            }
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
    
            idText = (EditText) findViewById(R.id.id);
            passText = (EditText) findViewById(R.id.pass);
    
    
    
        }
    
        public void login(View v) {
    
            if (database != null) {
                Cursor cursor = database.rawQuery("SELECT name, num, pass FROM " + tableName, null);
                int count = cursor.getCount();
                for(int i=0;i<count;i++) {
                    cursor.moveToNext();
    
                    Cname = cursor.getString(0);
                    Cnum = cursor.getString(1);
                    Cpass = cursor.getString(2);
                }
                Id = idText.getText().toString();
                Pass = passText.getText().toString();
                if (Id.equals(Cnum) && Pass.equals(Cpass)) {
                    Intent main = new Intent(getApplication(), Main.class);
                    main.putExtra("splash", "splash");
                    startActivity(main);
                    finish();
                    Toast.makeText(getApplicationContext(), Cname + "님 환영합니다.",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "정확한 정보를 입력하세요.",
                            Toast.LENGTH_SHORT).show();
                }
                cursor.close();
            }
    
        }
    
    
    
    
    
    
                public void member(View view){
    
                Intent member = new Intent(getApplication(), Member.class);
               member.putExtra("splash", "splash");
                startActivity(member);
    
                  }
    }
    

    마지막으로 회원가입 폼이 있는 member.java 파일입니다.

    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.Toast;
    
    public class Member extends Info {
        EditText NAME,PASS,NUM,MAJOR;
        String Tname, Tpass, Tnum,Tmajor;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.member);
            NAME = (EditText) findViewById(R.id.name);
            PASS = (EditText) findViewById(R.id.password);
            NUM = (EditText) findViewById(R.id.num);
            MAJOR = (EditText) findViewById(R.id.major);
    
    
            ImageButton join = (ImageButton) findViewById(R.id.join);
            join.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Tname = NAME.getText().toString();
                    Tpass = PASS.getText().toString();
                    Tnum = NUM.getText().toString();
                    Tmajor = MAJOR.getText().toString();
                    Cursor cursor = database.rawQuery("SELECT name, num, major FROM " + tableName, null);
                    int count = cursor.getCount();
                    for(int i=0;i<count;i++) {
                        cursor.moveToNext();
    
                        Cname = cursor.getString(0);
                        Cnum = cursor.getString(1);
                        Cmajor = cursor.getString(2);
    
                    }
                    if (Tname.length()<2) {
                        Toast.makeText(getApplicationContext(), "이름을 정확하게 입력해주세요.",
                                Toast.LENGTH_SHORT).show();
                    } else if (Tpass.length() <6) {
                        Toast.makeText(getApplicationContext(), "비밀번호를 6자리 이상 입력하세요.",
                                Toast.LENGTH_SHORT).show();
                    }else if (Tnum.length() <10 || Tname.equals(Cnum)) {
                        Toast.makeText(getApplicationContext(), "이미 등록된 학번이거나 정확하지 않습니다.",
                                Toast.LENGTH_SHORT).show();
                    } else if (Tmajor.length() <3) {
                        Toast.makeText(getApplicationContext(), "학과이름을 정확히 입력해주세요.",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        try{
                            if (database != null) {
                                database.execSQL("INSERT INTO " + tableName + "(name, pass, num, major) VALUES" +
                                       "(" + "'" + Tname + "'" + "," + "'" + Tpass + "'" + "," + Tnum + "," + "'" + Tmajor + "'" + ")");
                            }
    
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Intent login = new Intent(getApplication(), Login.class);
                        login.putExtra("splash", "splash");
                        startActivity(login);
                        finish();
                            Toast.makeText(getApplication(), Tname + "님 회원가입을 축하합니다.", Toast.LENGTH_SHORT).show();
                    }
                } });
        }
    }