1. 程式人生 > >Android+SQlite 小型管理系統

Android+SQlite 小型管理系統

      Android學習過程中,結合sqlite資料庫完成小型的人員管理系統,儘管專案比較小,但是整體思路比較清晰,功能比較實用,適合新手學習。下面是效果圖:

專案整體架構:


部分程式碼:

dbHelper.java

public class dbHelper extends SQLiteOpenHelper {

	private static final String DB_NAME = "data.db";
	private static final int VERSION = 1;
	private static final String SWORD="SWORD";
	
	public dbHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
		// TODO Auto-generated constructor stub
	}

	//create the db
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
        Log.i(SWORD, "create a database");
        
        String sql="create table user(name varchar(20) not null ," +
        		" password varchar(60) not null );";
        db.execSQL(sql);
        
        sql="create table client(name varchar(20) not null,"
                +"age varchar(10),detail varchar(100),"
                +"address varchar(50),phone varchar(12));";
        db.execSQL(sql);
	}

	//update a db
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
        Log.i(SWORD, "update a db");
	}

}

Regester.java
        register.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String edtName = name.getText().toString();
				String edtPass = pass.getText().toString();
				
				if(!(edtName.equals("") && edtPass.equals(""))){
					if(addUser(edtName,edtPass)){
					   Toast.makeText(RegisterActivity.this, "註冊成功!", Toast.LENGTH_SHORT).show();
					   //跳轉到登入成功介面
					   Intent in = new Intent();
					   in.setClass(getApplicationContext(), MainActivity.class);
					   startActivity(in);
					   RegisterActivity.this.finish();
					}
					else{
						new AlertDialog.Builder(RegisterActivity.this)
						.setTitle("註冊失敗").setMessage("註冊失敗")
						.setPositiveButton("OK", null).show();
					}
				}
				else{
					new AlertDialog.Builder(RegisterActivity.this)
					.setTitle("賬號密碼不能為空").setMessage("賬號密碼不能為空")
					.setPositiveButton("OK", null).show();
				}
			}

			private boolean addUser(String name, String pass) {
				// TODO Auto-generated method stub
				String str = "insert into user values(?,?)";
				
				database = new dbHelper(getApplicationContext());
				SQLiteDatabase db=database.getReadableDatabase();
				try{
					db.execSQL(str,new String[]{name,pass});
					return true;
				}
				catch(Exception e){
					Log.i("RegisterActivity", "adduser-->error");
					
				}
				return false;
			}
        	
        });

AddActivity.java
	 protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	        setContentView(R.layout.add);
	        
	        findViews();
	        
	        save.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					
					String strName = name.getText().toString();
					String strAge = age.getText().toString();
					String strDetail = detail.getText().toString();
					String strAddress = address.getText().toString();
					String strPhone = phone.getText().toString();
					
					helper = new dbHelper(getApplication());
					SQLiteDatabase db = helper.getReadableDatabase();
					String str="insert into client values(?,?,?,?,?)";
					try{
					   db.execSQL(str, new String[]{strName,strAge,strDetail,strAddress,strPhone});
					   Intent in = new Intent();
					   in.setClass(getApplicationContext(), WelcomeActivity.class);
					   startActivity(in);
					   AddActivity.this.finish();
					}
					catch(Exception e){
						Log.i("AddActivity", "add---error");
					}
				}
	        });
	 }

原始碼下載