2017-03-15 35 views
0

(編輯:經過各種嘗試使代碼工作...沒有什麼。幾個建議後,沒有工作,我仍然在尋找回答:)android.database.sqlite.SQLiteException:表Registerd_Users沒有名爲userType的列

private static final String KEY_ID = "id"; 
private static final String KEY_FNAME = "fullName"; 
private static final String KEY_EMAIL = "userEmail"; 
private static final String KEY_PASS = "passWord"; 
private static final String KEY_TYPE = "userType"; 

但是在運行時,程序告訴我沒有UserType這樣的列。 這是兩個Java類文件的代碼。

DatabaseHandler.Java

package com.set.ultimax.login; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseHandler extends SQLiteOpenHelper { 

// All Static variables 
// Database Version 
private static final int DATABASE_VERSION = 1; 

// Database Name 
private static final String DATABASE_NAME = "Users"; 

private static final String TABLE_USERS = "Registerd_Users"; 

// Account Table Columns names 
private static final String KEY_ID = "id"; 
private static final String KEY_FNAME = "fullName"; 
private static final String KEY_EMAIL = "userEmail"; 
private static final String KEY_PASS = "passWord"; 
private static final String KEY_TYPE = "userType"; 

public DatabaseHandler(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

// Creating Tables 
@Override 
public void onCreate(SQLiteDatabase db) { 
    String CREATE_ACCOUNT_TABLE = "CREATE TABLE " + TABLE_USERS + "(" 
      + KEY_ID + " INTEGER PRIMARY KEY," + KEY_FNAME + " TEXT," + KEY_EMAIL + " TEXT," 
      + KEY_PASS + " TEXT " + KEY_TYPE + " TEXT" + ")"; 
    db.execSQL(CREATE_ACCOUNT_TABLE); 
} 

// Upgrading database 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // Drop older table if existed 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS); 
    onCreate(db);// Create tables again 
} 

// Adding new User 
void addUsers(Users users) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_FNAME, users.getFname()); //full name 
    values.put(KEY_EMAIL, users.getEmail()); // Email 
    values.put(KEY_PASS, users.getPassword()); // Password 
    values.put(KEY_TYPE, users.getuserType()); //user Type 

    // Inserting Row 
    db.insert(TABLE_USERS, null, values); 
    db.close(); // Closing database connection 
} 
public void deleteAll() //Deletes all data in the database 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_USERS,null,null); 
} 

public boolean validateUser(String fullName, String userEmail, String password, String userType){ 
    Cursor c = getReadableDatabase().rawQuery(
      "SELECT * FROM " + TABLE_USERS + " WHERE " 
        + KEY_FNAME + "='" + fullName + "'AND " 
        + KEY_EMAIL + "='" + userEmail +"'AND " 
        + KEY_PASS + "='" + password + "'AND " 
        + KEY_TYPE + "='" + userType + "'" , null); 
    if (c.getCount() > 0) { 
     return true; 
    } 
    else{return false;} 

} 
public boolean sameUser(String userEmail){ 
    Cursor c = getReadableDatabase().rawQuery(
      "SELECT * FROM " + TABLE_USERS + " WHERE " 
        + KEY_EMAIL + "='" + userEmail + "'" , null); 
    if (c.getCount() > 0) { 
     return true; 
    } 
    else{return false;} 
} 

} 

SignUp.Java

package com.set.ultimax.login; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Paint; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class SignUp extends Activity { 

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

    TextView goHome = (TextView) findViewById(R.id.home); 
    Button register = (Button) findViewById(R.id.reg); 

    goHome.setPaintFlags(goHome.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG); 

    goHome.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 
      Intent toMain = new Intent(SignUp.this, Main.class); 
      startActivity(toMain); 
     } 
    }); 

    register.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 
      DatabaseHandler db = new DatabaseHandler(v.getContext()); 
      Intent toMain = new Intent(SignUp.this, Main.class); 
      EditText fName = (EditText) findViewById(R.id.fName); 
      EditText email = (EditText) findViewById(R.id.Email); 
      EditText pass = (EditText) findViewById(R.id.password); 
      String fNameValue = fName.getText().toString(); 
      String emailValue = email.getText().toString(); 
      String passValue = pass.getText().toString(); 

      int charemailLength = emailValue.length(); 
      int charPassLength = passValue.length(); 
      boolean emailMatch = db.sameUser(emailValue); 

      if (emailMatch) { //Checks to see if the UserName already exists 
       Toast.makeText(SignUp.this, "UserName Already Taken", Toast.LENGTH_LONG).show(); 
      } 
      else if (emailValue.equals("") && passValue.equals("")) { 
       Toast.makeText(SignUp.this, "Fields Empty", Toast.LENGTH_SHORT).show(); 
      } 
      else if (charemailLength <= 5 && charPassLength <= 5) { 
        Toast.makeText(SignUp.this, "Characters too short", Toast.LENGTH_SHORT).show(); 
       } 
      else { 
       db.addUsers(new Users(fNameValue, emailValue, passValue,"Admin")); 
       startActivity(toMain); 
      } 

     } 

    }); 

} 
} 

我一直在玩的代碼進行約5小時,並通過我的狹隘的疲憊,我看不出有什麼錯誤。任何幫助都會很棒。

非常感謝你提前!

+0

嘗試卸載您的應用程序並重新安裝。因爲我看不到任何錯誤 –

+0

我這樣做,它沒有工作,謝謝你的迴應,雖然 – RideTheMountainOfBlue

回答

0
 + KEY_PASS + " TEXT " + KEY_TYPE + " TEXT" + ")"; 
          ^^ 

缺少逗號。

+0

你是怎麼看到的?我會重試它,並告訴你我是怎麼做到的。順便謝謝你! – RideTheMountainOfBlue

相關問題