看了很多別人寫的安卓SQlite數據的操作代碼,一點也不通俗易懂,我覺得我寫的不錯,而且安卓項目也用上了,所以在博客園里保存分享一下!
建立一個類 並繼承SQLiteOpenHelper
public class MySqliteHelper extends SQLiteOpenHelper { public MySqliteHelper(Context context) { super(context, "userdb.db", null, 3); // TODO Auto-generated constructor stub }
在oncreate()函數中建立表
public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("create table users(id int primary key ,name text,pwd text)"); }
在方法中建立db文件
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table users(id integer primary key ,name text,pwd text)");
String sql = "insert into users values (0,0,0)"; //id 自增加
db.execSQL(sql);
}
整體代碼:
package zcd.sqlite; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast; public class MySqliteHelper extends SQLiteOpenHelper { public MySqliteHelper(Context context) { super(context, "userdb.db", null, 3); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("create table users(id integer primary key ,name text,pwd text)"); String sql = "insert into users values (0,0,0)"; //id 自增加 db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
然后在自己另外一個類中實現數據庫表的增刪改查。
我用的是Fragment,用Activity的話吧getActivity()去掉,換成this
實現了用戶登錄注冊的功能!
http://www.cnblogs.com/zhaocundang QQ463431476 個人原創代碼,不會的可以問我。
因為建立的數據庫里面的數據沒有判斷數據的唯一性,所以在代碼中實現了數據唯一性的判斷。實現登錄注冊代碼設計主要是在插入數據和查找數據的基礎上做一個用戶名是否存在,或者用戶名和用戶密碼是否輸入正確、輸入錯誤的判斷。
代碼中定義一個標示判斷 用戶名是否存在int userflag 定義一個登錄時判斷用戶和密碼是否輸入正確的標識int loginflag
注冊時先判斷用戶名是否存在,userflag=1不存在此用戶可以插入數據,userflag=0此用戶存在跳出循環。
登錄時loginflag為1用戶登錄成功,loginflag為0出現用戶名或者密碼輸入錯誤的提示。
具體代碼:
自己看,寫的很詳細了。
這里用了安卓5.0的沉浸式操作欄,可以忽略去掉,看主要的部分啊!!
package zcd.sqlite; import zcd.main.MainActivity; import zcd.netanything.R; import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Userlogin extends Activity implements OnClickListener{ private MySqliteHelper helper; Button sign; Button reg; String name; String mypwd; private EditText user; private EditText pwd; int userflag ;//定義一個標示判斷 用戶名是否存在 int loginflag ;//登錄時判斷用戶密碼是否輸入正確 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //設置狀態欄顏色 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); getWindow().setStatusBarColor(getResources().getColor(R.color.StatusBar)); //設置actionbar顏色 ActionBar actionBar = getActionBar(); actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0099CC"))); setContentView(R.layout.login); findViewById(R.id.button1).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); user = (EditText)findViewById(R.id.editText1); pwd=(EditText)findViewById(R.id.editText2); } public void insert() { helper = new MySqliteHelper(getApplicationContext()); SQLiteDatabase db=helper.getWritableDatabase(); //建立打開可讀可寫的數據庫實例 //查詢一下,是否用戶名重復 String sql1 = "select * from users"; Cursor cursor = db.rawQuery(sql1, null); while (cursor.moveToNext()) { //第一列為id name = cursor.getString(1); //獲取第2列的值,第一列的索引從0開始 mypwd = cursor.getString(2);//獲取第3列的值 if((user.getText().toString().isEmpty())||(pwd.getText().toString().isEmpty())){ Toast.makeText(this, "不能為空,請重新輸入", Toast.LENGTH_SHORT).show(); break; } userflag = 1; //不存在此用戶 if((user.getText().toString().equals(name))) { Toast.makeText(this, "已存在此用戶,請重新注冊", Toast.LENGTH_SHORT).show(); userflag =0; break; } } if(userflag==1) { String sql2 = "insert into users(name,pwd) values ('"+user.getText().toString()+"','"+pwd.getText().toString()+"')"; db.execSQL(sql2); Toast.makeText(this, "注冊成功!", Toast.LENGTH_SHORT).show(); } } public void select() { helper = new MySqliteHelper(getApplicationContext()); SQLiteDatabase db=helper.getWritableDatabase(); String sql = "select * from users"; Cursor cursor = db.rawQuery(sql, null); while (cursor.moveToNext()) { //第一列為id name = cursor.getString(1); //獲取第2列的值,第一列的索引從0開始 mypwd = cursor.getString(2);//獲取第3列的值 if((user.getText().toString().equals(name))&&(pwd.getText().toString().equals(mypwd))) { Toast.makeText(this, "用戶驗證成功", Toast.LENGTH_SHORT).show(); loginflag=1; //intent bundle傳值 Intent MainActivity = new Intent(); MainActivity .setClass(this,MainActivity.class); Bundle bundle = new Bundle(); //該類用作攜帶數據 bundle.putString("user", user.getText().toString()); MainActivity.putExtras(bundle); //向MainActivity傳值 this.startActivity(MainActivity); finish();//退出 } } if((user.getText().toString().isEmpty())||(pwd.getText().toString().isEmpty())){ Toast.makeText(this, "不能為空,請重新輸入", Toast.LENGTH_SHORT).show(); } if(loginflag!=1) { Toast.makeText(this, "賬號或者密碼錯誤,請重新輸入", Toast.LENGTH_SHORT).show(); } cursor.close(); db.close(); //Toast.makeText(this, "已經關閉數據庫", Toast.LENGTH_SHORT).show(); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.button1: select(); break; case R.id.button2: insert(); break; } } }
效果如下:

默認是插入0,0,0然后再插入數據后id號就自動增加了.
這是我的方法,不過寫autoincreatement SQLite3不好使,如果你能夠實現就回復一下吧!