嗨,我想知道如何将数据保存在 sqlite 数据库中,以及如何比较该数据库中的数据以达到目的,假设我有一个注册页面,注册后我有一个登录页面,并且我想登录该登录页面转到下一个活动,我在 ListView 中有那个人的数据并登录应该只通过数据库完成,请帮帮我
适配器类
public class DBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_EMAIL = "email";
public static final String KEY_PHONE = "phone";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "MyDB";
private static final String DATABASE_TABLE = "contacts";
private static final int DATABASE_VERSION = 4;
private static final String DATABASE_CREATE =
"create table contacts (_id integer primary key autoincrement, "
+ "name text not null, email text not null, phone int not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);//SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
} // SQLiteDatabase.CursorFactory is null by default
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a contact into the database---
public long insertContact(String name, String email, String phone)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_EMAIL, email);
initialValues.put(KEY_PHONE, phone);
return db.insert(DATABASE_TABLE, null, initialValues); // 2nd parameter - null indicates all column values.
}
//---deletes a particular contact---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; // relational operator returns true / false
}
//---retrieves all the contacts---
public Cursor getAllContacts()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,KEY_EMAIL,KEY_PHONE}, null, null, null, null, null);
}
//---retrieves a particular contact---
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_EMAIL,KEY_PHONE}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a contact---
public boolean updateContact(long rowId, String name, String email, String phone)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_EMAIL, email);
args.put(KEY_PHONE, phone);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
主要活动课
public class MainActivity extends Activity
{
Button submit;
EditText name,email,number;
private long id;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button)findViewById(R.id.button1);
name = (EditText)findViewById(R.id.editText1);
email = (EditText)findViewById(R.id.editText2);
number = (EditText)findViewById(R.id.editText3);
// final String name1 = name.getText().toString();
// final String email1 = email.getText().toString();
// final String number1 = number.getText().toString();
final DBAdapter db = new DBAdapter(this);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
String name1 = name.getText().toString();
String email1 = email.getText().toString();
String number1 = number.getText().toString();
db.open();
id = db.insertContact(name1,email1,number1);
//id = db.insertContact("Suven Consultants", "courses.suvenconsultants.com","+919892544177");
db.close();
db.open();
Cursor c = db.getContact(id);
if (c.moveToFirst())
DisplayContact(c);
else
//Toast.makeText(this, "No contact found", Toast.LENGTH_LONG).show();
db.close();
}
});
}
public void DisplayContact(Cursor c)
{
Toast.makeText(this, "id: " + c.getString(0) + "\n" +
"Name: " + c.getString(1) + "\n" +
"Email: " + c.getString(2)+ "\n" +
"Phone: " + c.getString(3),
Toast.LENGTH_LONG).show();
}
}