表用户没有名为课程

时间:2016-01-25 17:22:36

标签: java android database sqlite android-sqlite

DatabaseHandler.Java

    private static final int DATABASE_Version=4;
    private static final String DATABASE_Name="UserManager";
    private static final String TABLE_USERS="Users";
    private SQLiteDatabase db;
    private static final String USER_NAME ="Name";
    private static final String USER_PASSWRD ="Passwrod";
    private static final String USER_REPASSWRD ="ReEnterPassword";
    private static final String USER_EMAIL ="Email";
    private static final String USER_AGE ="Age";
    private static final String USER_PHONENO ="PhoneNumber";
    private static final String USER_COLLEGE ="College";
    private static final String USER_COURSE ="Course";
    public DatabaseHandler(Context context){
        super(context, DATABASE_Name, null, DATABASE_Version);
    }
    public void onCreate(SQLiteDatabase db) {
        String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS + "("
                + USER_NAME + " TEXT," + USER_PASSWRD + " TEXT,"
                + USER_REPASSWRD + " TEXT," + USER_EMAIL + " TEXT PRIMARY KEY,"  + USER_PHONENO +" INTEGER," + USER_COLLEGE + " TEXT,"
                 + USER_COURSE + " TEXT);" ;
        db.execSQL(CREATE_USERS_TABLE);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);

        // Create tables again
        onCreate(db);
    }
    public void addUser(signup users) {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(USER_NAME, users.getName()); // Contact Name
        values.put(USER_PASSWRD, users.getPasswrd());
        values.put(USER_REPASSWRD, users.getRepaswrd());
        values.put(USER_EMAIL, users.getEmail());
        values.put(USER_AGE, users.getAge());
        values.put(USER_PHONENO, users.getPhoneno());
        values.put(USER_COLLEGE, users.getCollege());
        values.put(USER_COURSE, users.getCourse()); // Contact Phone Number
        // Inserting Row
        db.insert(TABLE_USERS, null, values);
        db.close(); // Closing database connection
    }


    public void open()
    {
       db = this.getWritableDatabase();
    }


    public void close()
    {
        this.close();
    }

    public boolean Login(String username, String password) throws SQLException
    {
        db = null;
        Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_USERS + " WHERE ``username=? AND password=?", new String[]{username,password});
        if (mCursor != null) {
            if(mCursor.getCount() > 0)
            {
                return true;
            }
        }
        return false;
    }

RegisterActivity.Java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        final DatabaseHandler db=new DatabaseHandler(this);

        final TextView name1 = (TextView) findViewById(R.id.name);


        final TextView pwd1 = (TextView) findViewById(R.id.paswrd);
        final TextView cnfrm1 = (TextView) findViewById(R.id.repaswrd);
        final TextView email1 = (TextView) findViewById(R.id.email);
        final TextView age1 = (TextView) findViewById(R.id.age);
        final TextView phone1 = (TextView) findViewById(R.id.phone);
        final TextView college1 = (TextView) findViewById(R.id.college);
        final TextView course1 = (TextView) findViewById(R.id.course);



        Button signup = (Button) findViewById(R.id.button);
        View.OnClickListener sp = new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {

                String name = name1.getText().toString();
                String pwd = pwd1.getText().toString();
                String cnfrm = cnfrm1.getText().toString();
                String email = email1.getText().toString();
                String age = age1.getText().toString();
                String phone = phone1.getText().toString();
                String college = college1.getText().toString();
                String course = course1.getText().toString();

                if (name.isEmpty()) {
                    name1.setError("Enter Name");
                } else if (pwd.isEmpty()) {
                    pwd1.setError("Enter Password");
                } else if (cnfrm.isEmpty()) {
                    cnfrm1.setError("Enter Confirm Password ");
                } else if (email.isEmpty()) {
                    email1.setError("Enter Email");
                } else if (age.isEmpty()) {
                    age1.setError("Enter Age");
                } else if (phone.isEmpty()) {
                    phone1.setError("Enter Phone Number");
                } else if (college.isEmpty()) {
                    college1.setError("Enter College name");
                } else if (course.isEmpty()) {
                    course1.setError("Enter Course");
                } else if (!(pwd.equals(cnfrm))) {
                    cnfrm1.setError("Password doesn't Match");
                } else {

                    String name_1= name1.getText().toString();
                    String pass_1= pwd1.getText().toString();
                    String repass_1= cnfrm1.getText().toString();
                    String email_1= email1.getText().toString();
                    String age_1= age1.getText().toString();
                    String phoneno_1= phone1.getText().toString();
                    String clg_1= college1.getText().toString();
                    String course_1= course1.getText().toString();
                    Log.d("Insert: ", "Inserting ..");
                    db.addUser(new signup(name_1, pass_1, repass_1, email_1, age_1, phoneno_1, clg_1, course_1));

                     Toast.makeText(getApplicationContext(), "Successfully Signed in", Toast.LENGTH_LONG).show();
                    Intent intent=new Intent(RegisterActivity.this,FirstActivity.class);

                    startActivity(intent);
                }
            }
        };

        signup.setOnClickListener(sp);
        Button reset=(Button) findViewById(R.id.rest);
        View.OnClickListener rs= new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((TextView) findViewById(R.id.name)).setText(null);
                ((TextView) findViewById(R.id.course)).setText(null);
                ((TextView) findViewById(R.id.email)).setText(null);
                ((TextView) findViewById(R.id.age)).setText(null);
                ((TextView) findViewById(R.id.college)).setText(null);
                ((TextView) findViewById(R.id.paswrd)).setText(null);
                ((TextView) findViewById(R.id.repaswrd)).setText(null);
                ((TextView) findViewById(R.id.phone)).setText(null);
            }
        };
        reset.setOnClickListener(rs);
    }

signup.Java

public class signup {
    String name;
    String passwrd;
    String repaswrd;
    String email;
    String age;
    String phoneno;
    String college;
    String course;


    public signup(String name,String passwrd,String repaswrd,String email,String age,String phoneno,String college,String course){
        this.name=name;
        this.passwrd=passwrd;
        this.repaswrd=repaswrd;
        this.email=email;
        this.age=age;
        this.phoneno=phoneno;
        this.college=college;
        this.course=course;

    }

    public signup(String name,String passwrd,String repaswrd,String email,String college,String course){
        this.name=name;
        this.passwrd=passwrd;
        this.repaswrd=repaswrd;
        this.email=email;
        this.college=college;
        this.course=course;

    }


    public String getName(){
        return this.name;
    }
    public String getPasswrd(){
        return this.passwrd;
    }
    public String getRepaswrd(){
        return this.repaswrd;
    }
    public String getEmail(){
        return this.email;
    }
    public String getAge(){
        return this.age;
    }
    public String getPhoneno(){
        return this.phoneno;
    }
    public String getCollege(){
        return this.college;
    }
    public String getCourse(){
        return this.course;
    }
}

错误:

  

E / SQLiteLog:(1)表用户没有名为Course的列       01-25 22:45:31.414 12064-12064 / com.pixel.sri.justdoit E / SQLiteDatabase:插入课程时出错= B.E ReEnterPassword = sri   Passwrod = sri Email = sriram.mdu31@gmail.com College = APEC   PhoneNumber = 7402043073年龄= 21姓名= sriram                                                                               android.database.sqlite.SQLiteException:table用户没有列   命名课程(代码1):,编译时:INSERT INTO   用户(场,ReEnterPassword,Passwrod,电子邮件,学院,******中国,年龄,姓名)   价值观(?,?,?,?,?,?,?,?)                                                                                   在   android.database.sqlite.SQLiteConnection.nativePrepareStatement(母语   方法)                                                                                   在   android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)                                                                                   在   android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)                                                                                   在   android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)                                                                                   在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)                                                                                   在   android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)                                                                                   在   android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)                                                                                   在   android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)                                                                                   在   com.pixel.sri.justdoit.DatabaseHandler.addUser(DatabaseHandler.java:66)                                                                                   在   com.pixel.sri.justdoit.RegisterActivity $ 1.onClick(RegisterActivity.java:81)                                                                                   在android.view.View.performClick(View.java:5201)                                                                                   在android.view.View $ PerformClick.run(View.java:21163)                                                                                   在android.os.Handler.handleCallback(Handler.java:746)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                   在android.os.Looper.loop(Looper.java:148)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5443)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)       01-25 22:45:31.572 12064-12089 / com.pixel.sri.justdoit V / RenderScript:0xb8b446f0启动线程,CPU 8

2 个答案:

答案 0 :(得分:1)

您可能在上次运行之后添加了列,此时数据库已经创建。

解决方案:卸载并重新安装您的应用 替代解决方案:增加DATABASE_VERSION常量值。

答案 1 :(得分:1)

如果Course列是数据库的最新添加项,但是您忘记增加数据库版本,则这种情况很常见。

使用您拥有的代码,碰撞数据库版本将擦除数据库并使用正确的列重新创建数据库。

或者,卸载并重新安装您的应用应该修复它。