(编辑:经过各种尝试使代码正常工作......没什么。在一些没有成功的建议之后,我仍在寻找答案:)
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)
+ KEY_PASS + " TEXT " + KEY_TYPE + " TEXT" + ")";
^^
缺少逗号。