如何在sqlite db中永久插入用户名和密码

时间:2017-02-27 20:26:55

标签: android android-studio android-sqlite

嘿伙计们,我想在sqlite中永久插入我的用户名和密码,所以当我在其他手机中安装它时,它会在那里,我可以使用我的数据库中插入的用户名和密码登录。怎么做?

public class DatabaseHelper extends SQLiteOpenHelper{

public DatabaseHelper(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}
SQLiteDatabase db;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_UNAME = "uname";
private static final String COLUMN_PASS = "pass";

private static final String COLUMN_BIRTH = "birth";
private static final String COLUMN_PHONE = "phone";
private static final String COLUMN_COURSE = "course";
private static final String TABLE_CREATE = "create table if not exists contacts (id integer primary key not null, name text not null, email text not null, uname text not null, pass text name not null);";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME,null,DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    this.db = db;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
    db.execSQL(query);
    this.onCreate(db);
}
public void insertContact(Contact c)
{
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    String query = "select * from contacts";
    Cursor cu = db.rawQuery(query, null);
    int count = cu.getCount();

    values.put(COLUMN_ID, count);

    values.put(COLUMN_NAME, c.getName());
    values.put(COLUMN_EMAIL, c.getEmail());
    values.put(COLUMN_UNAME, c.getUname());
    values.put(COLUMN_PASS, c.getPass());
    values.put(COLUMN_BIRTH, c.getBirth());
    values.put(COLUMN_PHONE, c.getPhone());
    values.put(COLUMN_COURSE, c.getCourse());
    db.insert(TABLE_NAME, null, values);
    db.close();
}
public String searchPass(String uname)
{
    db = this.getReadableDatabase();
    String query = "select uname, pass from "+ TABLE_NAME;
    Cursor cu = db.rawQuery(query, null);
    String a,b = null;
    if(cu.moveToFirst())
    {
        do{
            a = cu.getString(0);

            if(a.equals(uname))
            {
                b = cu.getString(1);
                break;
            }
        }
        while(cu.moveToNext());

    }
    return b;

}

} 谢谢你的帮助......

1 个答案:

答案 0 :(得分:1)

有两种方法: 在创建表插入记录后,首先使用onCreate()的{​​{1}}方法。

其次是分别创建一个包含数据的SQLite数据库文件。并将其放在资产文件夹中,当应用程序启动时,检查数据库是否存在,如果没有,则复制到本地数据库。请注意,在此方法中,不要在DatabaseHelper中编写创建表查询。

修改

第一种方式:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    this.db = db;
    Contact c=new Contact();
    // set your data
    insertContact(c);
}

第二种方式:

请点击此链接:Copy Database from assets folder in unrooted device