我正在开发我的第一个应用程序,用户必须在其中登录。我正在使用 SQLite 数据库存储用户名和密码。但是当我尝试向数据库添加新联系人时出现强制关闭错误。请帮助我我在哪里错了。以下是代码。
User.java 文件主要用于 getter 和 setter:
package com.example.first_db_app;
public class User
{
//private variables
int _userid;
String _uname;
String _upwd;
//empty constructor
public User()
{
}
//constructor
/*public User(int userid,String uname,String upwd)
{
this._userid=userid;
this._uname=uname;
this._upwd=upwd;
}*/
//constructor
public User(String uname,String upwd)
{
this._uname=uname;
this._upwd=upwd;
}
//getting userid
public int getUserId()
{
return this._userid;
}
//setting userid
public void setUserId(int userid )
{
this._userid=userid;
}
//getting username
public String getUname()
{
return this._uname;
}
//setting username
public void setUname(String uname)
{
this._uname=uname;
}
//getting user password
public String getUpwd()
{
return this._upwd;
}
//setting user password
public void setUpwd(String upwd)
{
this._upwd=upwd;
}
}
我在其中创建了 DB 和 CRUD 操作的 DatabaseHandler.java
package com.example.first_db_app;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
//creating database
public class DatabaseHandler extends SQLiteOpenHelper
{
//private static variables
private static final int DATABASE_VERSION=1;
//database name
private static final String DATABASE_NAME="UserDB";
//table name
private static final String TABLE_NAME="Users";
//table column names
private static final String USER_ID="id";
private static final String USER_NAME="u_name";
private static final String USER_PWD="u_pwd";
public DatabaseHandler(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
//creating tables
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
String CREATE_USERS_TABLE= "CREATE TABLE"+ TABLE_NAME+ "("+ USER_NAME + "TEXT,"+ USER_PWD + "TEXT" + ")";
db.execSQL(CREATE_USERS_TABLE);
}
//upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
//drop older table if existed
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
//creates table again
onCreate(db);
}
//adding new users to the database
public void addUser(User user)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
//values.put(USER_ID, user.getUserId());
values.put(USER_NAME, user.getUname());
values.put(USER_PWD, user.getUpwd());
//inserting row
db.insert(TABLE_NAME, null, values);
db.close();
}
//reading a single row.this is done by using cursor
public String getUser(String Uname)
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.query(TABLE_NAME,new String[] {USER_NAME,USER_PWD},USER_NAME + "=?",new String[]{String.valueOf(Uname)},null,null,null,null);
if (cursor!=null)
cursor.moveToFirst();
//User user=new User(cursor.getString(0),cursor.getString(1));
String password=cursor.getString(cursor.getColumnIndex(USER_PWD));
cursor.close();
//return user
return password;
}
//Updating a single record in database
public int updateUser(User user)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(USER_NAME,user.getUname());
values.put(USER_PWD, user.getUpwd());
//updating row
db.update(TABLE_NAME, values, USER_NAME + "=?" ,new String[]{String.valueOf(user.getUname())});
return 0;
}
//Deleting a single record in database
public void deleteUser(User user)
{
SQLiteDatabase db=this.getWritableDatabase();
db.delete(TABLE_NAME, USER_NAME + "=?", new String[]{String.valueOf(user.getUname())});
db.close();
}
}
我使用以下文件进行用户登录,其中包含用户名和密码的文本框以及一个按钮
TestDbActivity.java
package com.example.first_db_app;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class TestDbActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_db);
DatabaseHandler db=new DatabaseHandler(this);
//CRUD operations
//inserting the records
Log.d("Insert: ", "Inserting ..");
db.addUser(new User("Ashwin","11111"));
db.addUser(new User("Ravi","22222"));
db.addUser(new User("Gopal","33333"));
db.addUser(new User("Satish","44444"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_db, menu);
return true;
}
}