[英]how to insert username and password permanently in sqlite db
大家好,我想在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;
}
} 謝謝您幫忙..
有兩種方法:第一種是在創建表后插入您的記錄,然后在DatabaseHelper
的onCreate()
方法中。
其次是分別創建一個包含您的數據的SQLite
數據庫文件。 並將其放在資產文件夾中,然后在啟動應用程序時檢查是否存在數據庫,如果不存在,則將其復制到本地數據庫中。 請注意,在這種方法中,不要在DatabaseHelper
編寫“創建表查詢”。
編輯
對於第一種方式:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
Contact c=new Contact();
// set your data
insertContact(c);
}
對於第二種方式:
單擊此鏈接: 從無根設備的資產文件夾中復制數據庫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.