0

嗨,我想知道如何将数据保存在 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();
}

}

4

3 回答 3

0

问题是您试图将字符串电话号码插入数据库的 int-Field 中,这是不可能的

改变这个:

private static final String DATABASE_CREATE = "创建表联系人(_id 整数主键自增," + "姓名文本不为空,电子邮件文本不为空,电话号码不为空);";

进入这个:

private static final String DATABASE_CREATE = "创建表联系人(_id 整数主键自增," + "姓名文本不为空,电子邮件文本不为空,电话文本不为空);";

那么它应该可以工作 不要将 int 用于数据库中的电话号码 int 仅限于一些可能导致错误的限制(例如,一个高到无法成为 int 值的数字),它更容易转换稍后在应用程序中来自数据库的字符串

此外,完成更改后不要忘记删除模拟器/设备上的旧数据库,否则不会执行新的创建语句。

于 2013-01-15T17:55:44.707 回答
0

为什么要使用int数据类型来存储电话号码?整数数据类型有最小和最大限制。由于电话号码超过 6 位,可能会引发Overflow Exception 您必须使用string数据类型将电话号码存储在数据库中。试试看

于 2013-07-30T05:53:15.740 回答
0

听起来您应该使用可以检查用户是否已注册然后验证信息的网络服务器来执行此操作。如果您只是让用户创建一个帐户并将该数据存储到手机以进行比较,只需使用SharedPreferences. 它比 SQLite 容易得多,您可以存储字符串键值对并轻松读取它以进行比较。

于 2013-01-15T18:03:14.583 回答