最近初學android,作了實驗室老師給的基本任務,就是簡單的登錄和註冊,並能經過SQLite實現登錄,SQlLite是嵌入在安卓設備中的java
好了下面是主要代碼:android
數據庫的創建:sql
這裏我只是創建了一個用簡單的存儲用戶名和密碼的表單數據庫
MyDBHelper.javaide
public class MyDBHelper extends SQLiteOpenHelper { public static final String CREATE_USERDATA="create table userData(" + "id integer primary key autoincrement," +"nametext," +"password text)"; private Context mContext; public MyDBHelper(Context context, String name, SQLiteDatabase.CursorFactory cursorFactory,int version){ super(context,name,cursorFactory,version); mContext=context; } public void onCreate(SQLiteDatabase db){ db.execSQL(CREATE_USERDATA); } public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ //onCreate(db); } }
註冊:this
Main2Activity.java
public class Main2Activity extends AppCompatActivity {
private MyDBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
dbHelper = new MyDBHelper(this,"UserStore.db",null,1);
}
public void logon(View view){
//SQLiteDatabase db=dbHelper.getWritableDatabase();
EditText editText3=(EditText)findViewById(R.id.editText3);
EditText editText4=(EditText)findViewById(R.id.editText4);
String newname =editText3.getText().toString();
String password=editText4.getText().toString();
if (CheckIsDataAlreadyInDBorNot(newname)) {
Toast.makeText(this,"該用戶名已被註冊,註冊失敗",Toast.LENGTH_SHORT).show();
}
else {
if (register(newname, password)) {
Toast.makeText(this, "插入數據表成功", Toast.LENGTH_SHORT).show();
}
}
}
//向數據庫插入數據
public boolean register(String username,String password){
SQLiteDatabase db= dbHelper.getWritableDatabase();
/*String sql = "insert into userData(name,password) value(?,?)";
Object obj[]={username,password};
db.execSQL(sql,obj);*/
ContentValues values=new ContentValues();
values.put("name",username);
values.put("password",password);
db.insert("userData",null,values);
db.close();
//db.execSQL("insert into userData (name,password) values (?,?)",new String[]{username,password});
return true;
}
//檢驗用戶名是否已存在
public boolean CheckIsDataAlreadyInDBorNot(String value){
SQLiteDatabase db=dbHelper.getWritableDatabase();
String Query = "Select * from userData where name =?";
Cursor cursor = db.rawQuery(Query,new String[] { value });
if (cursor.getCount()>0){
cursor.close();
return true;
}
cursor.close();
return false;
}
}
登錄:spa
MainActivity.java
public class MainActivity extends AppCompatActivity { private MyDBHelper dbHelper; private EditText username; private EditText userpassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); dbHelper = new MyDBHelper(this,"UserStore.db",null,1); } //點擊註冊按鈕進入註冊頁面 public void logonClicked(View view){ Intent intent = new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } //點擊登陸按鈕 public void loginClicked(View view) { username=(EditText)findViewById(R.id.editText2); userpassword=(EditText)findViewById(R.id.editText); String userName=username.getText().toString(); String passWord=userpassword.getText().toString(); if (login(userName,passWord)) { Toast.makeText(MainActivity.this, "登錄成功(ZY,111)", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "登錄失敗", Toast.LENGTH_SHORT).show(); } } //驗證登陸 public boolean login(String username,String password) { SQLiteDatabase db = dbHelper.getWritableDatabase(); String sql = "select * from userData where name=? and password=?"; Cursor cursor = db.rawQuery(sql, new String[] {username, password}); if (cursor.moveToFirst()) { cursor.close(); return true; } return false; }
好了,就醬。code