下午好,
我正在尝试创建一个简单的用户个人资料,并允许用户在创建帐户后使用他们保存的用户名和密码登录。
帐户详细信息未成功保存,我收到错误消息,我设置的用户名和密码不匹配?
我无法在代码中看到错误,并且无法解决我的问题。非常感谢帮助。
数据库助手:
package com.new.app;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
{
super(context, name, factory, version);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);
}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");
// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// The simplest case is to drop the old table and create a new one.
_db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
// Create a new one.
onCreate(_db);
}
}
SignUPActivity:
package com.techblogon.loginexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SignUPActivity extends Activity
{
EditText editTextUserName,editTextPassword,editTextConfirmPassword, editTextCurrentWeight, editTextTargetWeight, editTextBMI;
Button btnCreateAccount;
LoginDataBaseAdapter loginDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
// get Instance of Database Adapter
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get Refferences of Views
editTextUserName=(EditText)findViewById(R.id.editTextUserName);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
editTextCurrentWeight=(EditText)findViewById(R.id.editTextCurrentWeight);
editTextTargetWeight=(EditText)findViewById(R.id.editTextTargetWeight);
editTextBMI=(EditText)findViewById(R.id.editTextBMI);
btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
String confirmPassword=editTextConfirmPassword.getText().toString();
String currentweight=editTextCurrentWeight.getText().toString();
String targetweight=editTextTargetWeight.getText().toString();
String bmi=editTextBMI.getText().toString();
// check if any of the fields are vacant
if(userName.equals("")||password.equals("")||confirmPassword.equals("")||currentweight.equals("")||targetweight.equals("")||bmi.equals(""))
{
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!password.equals(confirmPassword))
{
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
else
{
// Save the Data in Database
loginDataBaseAdapter.insertEntry(userName, password, currentweight, targetweight, bmi);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
loginDataBaseAdapter.close();
}
}
LoginDatabaseAdapter:
package com.techblogon.loginexample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class LoginDataBaseAdapter
{
static final String DATABASE_NAME = "Student.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"STUDENT"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String userName,String password, String currentweight, String targetweight, String bmi)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
newValues.put("CURRENTWEIGHT",currentweight);
newValues.put("TARGETWEIGHT",targetweight);
newValues.put("BMI",bmi);
// Insert the row into your table
db.insert("STUDENT", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int deleteEntry(String UserName)
{
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("STUDENT", where, new String[]{UserName}) ;
// Toast.makeText(context, "Number of Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String getSinlgeEntry(String userName)
{
Cursor cursor=db.query("STUDENT", null, " USERNAME=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
public void updateEntry(String userName,String password, String currentweight, String targetweight, String bmi)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD",password);
updatedValues.put("CURRENTWEIGHT",currentweight);
updatedValues.put("TARGETWEIGHT",targetweight);
updatedValues.put("BMI",bmi);
String where="USERNAME = ?";
db.update("STUDENT",updatedValues, where, new String[]{userName});
}
}
HomeActivity:
package com.techblogon.loginexample;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class HomeActivity extends Activity
{
Button btnSignIn,btnSignUp;
LoginDataBaseAdapter loginDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create a instance of SQLite Database
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get The Reference Of Buttons
btnSignIn=(Button)findViewById(R.id.buttonSignIN);
btnSignUp=(Button)findViewById(R.id.buttonSignUP);
// Set OnClick Listener on SignUp button
btnSignUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
/// Create Intent for SignUpActivity and Start The Activity
Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
startActivity(intentSignUP);
}
});
}
// Methos to handleClick Event of Sign In Button
public void signIn(View V)
{
final Dialog dialog = new Dialog(HomeActivity.this);
dialog.setContentView(R.layout.login);
dialog.setTitle("STUDENT");
// get the Refferences of views
final EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
final EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);
Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);
// Set On ClickListener
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get The User name and Password
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
// fetch the Password form database for respective user name
String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);
// check if the Stored password matches with Password entered by user
if(password.equals(storedPassword))
{
Toast.makeText(HomeActivity.this, "Login Successful, Welcome to UCC Pocket Coach, Get Fit Fast!", Toast.LENGTH_LONG).show();
dialog.dismiss();
Intent ii=new Intent(HomeActivity.this,MainMenu.class);
startActivity(ii);
}
else
{
Toast.makeText(HomeActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
}
@Override
protected void onDestroy() {
super.onDestroy();
// Close The Database
loginDataBaseAdapter.close();
}
}
我似乎在日志文件中收到此错误,并且不确定如何更改我的代码以确保其正确保存
错误:
02-25 15:55:21.798: E/SQLiteLog(24494): (1) table STUDENT has no column named TARGETWEIGHT
02-25 15:55:21.812: E/SQLiteDatabase(24494): Error inserting TARGETWEIGHT=79 USERNAME=111496858 PASSWORD=hurling1 CURRENTWEIGHT=75 BMI=22.4
02-25 15:55:21.812: E/SQLiteDatabase(24494): android.database.sqlite.SQLiteException: table STUDENT has no column named TARGETWEIGHT (code 1): , while compiling: INSERT INTO STUDENT(TARGETWEIGHT,USERNAME,PASSWORD,CURRENTWEIGHT,BMI) VALUES (?,?,?,?,?)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at com.techblogon.loginexample.LoginDataBaseAdapter.insertEntry(LoginDataBaseAdapter.java:56)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at com.techblogon.loginexample.SignUPActivity$1.onClick(SignUPActivity.java:62)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.view.View.performClick(View.java:4756)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.view.View$PerformClick.run(View.java:19749)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.os.Handler.handleCallback(Handler.java:739)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.os.Handler.dispatchMessage(Handler.java:95)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.os.Looper.loop(Looper.java:135)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at java.lang.reflect.Method.invoke(Native Method)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at java.lang.reflect.Method.invoke(Method.java:372)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-25 15:55:21.812: E/SQLiteDatabase(24494): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
所有人都非常感激!
答案 0 :(得分:2)
错误可以是这一行:
static final String DATABASE_CREATE = "create table "+"STUDENT"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text); ";
您的数据库中没有“CurrentWeight”和“TargetWeight”属性。 你应该改成这样的东西:
static final String DATABASE_CREATE = "create table "+"STUDENT"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text,CURRENTWEIGHT real,TARGETWEIGHT real,BMI real); ";
希望这有帮助。
答案 1 :(得分:1)
创建表(Create语句)时,不包括TARGETWEIGHT,CURRENTHEIGHT或BMI。在声明中包括这三列,你应该没事。
这也是错误信息所说的。它找不到TARGETWEIGHT列,因为它不存在。 :)