Example diagram:
Activity_main.xml:
<textview
Android:id= "@+id/t1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "20DP"
android:layout_margintop= "10DP"
android:layout_marginleft= "10DP"
android:text= "Name:"/>
<edittext
Android:id= "@+id/text_name"
Android:layout_width= "200DP"
android:layout_height= "Wrap_content"
android:hint= "Input Name"
android:layout_torightof= "@+id/t1"
android:layout_marginleft= "100DP"
android:layout_marginright= "10DP"
/>
<textview
Android:id= "@+id/t2"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Password:"
android:layout_margintop= "20DP"
android:layout_marginleft= "10DP"
android:layout_below= "@+id/t1"
Android:textsize= "20DP"/>
<edittext
Android:id= "@+id/text_password"
Android:layout_width= "200DP"
android:layout_height= "Wrap_content"
android:hint= "Input Password"
android:layout_below= "@+id/text_name"
android:layout_torightof= "@id/t2"
android:layout_marginleft= "65DP"
android:layout_marginright= "10DP"
/>
<button
Android:id= "@+id/btn_reg"
Android:layout_width= "128DP"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/t2"
android:layout_margintop= "30DP"
android:text= "Register"
/>
<button
Android:id= "@+id/btn_del"
Android:layout_width= "128DP"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/t2"
android:layout_margintop= "30DP"
android:layout_torightof= "@+id/btn_reg"
android:text= "Delete"
/>
<button
Android:id= "@+id/btn_sel"
Android:layout_width= "128DP"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/t2"
android:layout_margintop= "30DP"
android:layout_torightof= "@id/btn_del"
android:text= "Query"
/>
<linearlayout
Android:id= "@+id/layout"
android:layout_below= "@id/btn_sel"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
Android:layout_width= "Match_parent"
>
</LinearLayout>
</RelativeLayout>
Mainactivity.java:
public class Mainactivity extends appcompatactivity implements view.onclicklistener{
LinearLayout Layout;
EditText Name,password;
Button Bt_reg,bt_del,bt_sel;
List<user> userlist;
Private Mydatabasehelper DBHelper;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initview ();
}
private void Initview () {
layout= (LinearLayout) Findviewbyid (r.id.layout);
Name= (EditText) Findviewbyid (r.id.text_name);
Password= (EditText) Findviewbyid (R.id.text_password);
Bt_reg= (Button) Findviewbyid (R.id.btn_reg);
Bt_del= (Button) Findviewbyid (R.id.btn_del);
Bt_sel= (Button) Findviewbyid (R.id.btn_sel);
Bt_reg.setonclicklistener (this);
Bt_del.setonclicklistener (this);
Bt_sel.setonclicklistener (this);
Userlist=new arraylist<user> ();
}
public void OnClick (view view) {
Switch (View.getid ()) {
Case R.id.btn_reg:
if (Name.gettext (). ToString (). Matches ("[a-za-z]*") && Name.gettext (). ToString (). Length () > 5 && Name.gettext (). ToString (). Length () < && Password.gettext (). ToString (). Matches ("[0-9]*") && Password.gettext (). ToString (). Length () > 2 && password.gettext (). ToString (). Length () < 7) {
DBHelper = new Mydatabasehelper (This, "create_user.db", NULL, 1);
Sqlitedatabase db = Dbhelper.getreadabledatabase ();
Db.execsql ("INSERT into User (Name,password) VALUES (?,?)", New String[]{name.gettext (). toString (), Password.gettext () . toString ()});
Db.close ();
Toast.maketext (Mainactivity.this, "registered successfully", Toast.length_short). Show ();
} else
Toast.maketext (mainactivity.this, "Input not valid", Toast.length_short). Show ();
Break
Case R.id.btn_del:
if (Name.gettext (). ToString (). Length ()! = 0 && Password.gettext (). ToString (). Length ()! = 0) {
User user = null;
DBHelper = new Mydatabasehelper (This, "create_user.db", NULL, 1);
Sqlitedatabase db = Dbhelper.getreadabledatabase ();
cursor cursor = db.rawquery ("SELECT *" + "from user where Name=?", New String[]{name.gettext (). toString ()});
if (Cursor.movetofirst ()) {
int id = cursor.getint (cursor.getcolumnindex ("id"));
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
String password = cursor.getstring (cursor.getcolumnindex ("password"));
user = new User (ID, name, password);
Cursor.close ();
if (User.getpassword (). Equals (Password.gettext (). toString ())) {
Db.execsql ("Delete from user where name=?", New String[]{name.gettext (). toString ()});
Toast.maketext (mainactivity.this, "delete succeeded", Toast.length_short). Show ();
} else
Toast.maketext (mainactivity.this, "Incorrect password", Toast.length_short). Show ();
Db.close ();
}
}
else if (Name.gettext (). ToString (). Length ()! = 0 && Password.gettext (). ToString (). Length () = = 0) {
User user = null;
DBHelper = new Mydatabasehelper (This, "create_user.db", NULL, 1);
Sqlitedatabase db = Dbhelper.getreadabledatabase ();
cursor cursor = db.rawquery ("SELECT *" + "from user where Name=?", New String[]{name.gettext (). toString ()});
if (Cursor.movetofirst ()) {
int id = cursor.getint (cursor.getcolumnindex ("id"));
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
String password = cursor.getstring (cursor.getcolumnindex ("password"));
user = new User (ID, name, password);
Cursor.close ();
if (User.getname (). Equals (Name.gettext (). toString ())) {
Db.execsql ("Delete from user where name=?", New String[]{name.gettext (). toString ()});
Toast.maketext (mainactivity.this, "delete succeeded", Toast.length_short). Show ();
} else
Toast.maketext (mainactivity.this, "user does not exist", toast.length_short). Show ();
Db.close ();
}
}
Break
Case R.id.btn_sel:
Layout.removeallviews ();
if (Name.gettext (). ToString (). Length () = = 0 && password.gettext (). ToString (). Length () = = 0) {
DBHelper = new Mydatabasehelper (This, "create_user.db", NULL, 1);
Sqlitedatabase db = Dbhelper.getreadabledatabase ();
cursor cursor = db.rawquery ("SELECT * from user", null);
if (Cursor.movetofirst ()) {
do {
int id = cursor.getint (cursor.getcolumnindex ("id"));
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
String password = cursor.getstring (cursor.getcolumnindex ("password"));
User user = new user (ID, name, password);
Userlist.add (user);
} while (Cursor.movetonext ());
}
for (User user:userlist) {
TextView TV = new TextView (this);
Tv.settext (User.tostring ());
Tv.settextsize (20);
Layout.addview (TV);
}
Userlist.clear ();
Cursor.close ();
Db.close ();
} else if (Name.gettext (). ToString (). Length ()! = 0 && Password.gettext (). ToString (). Length () = = 0) {
DBHelper = new Mydatabasehelper (This, "create_user.db", NULL, 1);
Sqlitedatabase db = Dbhelper.getreadabledatabase ();
cursor cursor = db.rawquery ("SELECT *" + "from user where Name=?", New String[]{name.gettext (). toString ()});
User user = null;
if (Cursor.movetofirst ()) {
int id = cursor.getint (cursor.getcolumnindex ("id"));
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
String password = cursor.getstring (cursor.getcolumnindex ("password"));
user = new User (ID, name, password);
Cursor.close ();
Db.close ();
TextView TV = new TextView (this);
Tv.settext (User.tostring ());
Tv.settextsize (20);
Layout.addview (TV);
}
}
else if (Name.gettext (). toString (). Length ()!=0) {
Dbhelper=new Mydatabasehelper (This, "create_user.db", null,1);
Sqlitedatabase db=dbhelper.getreadabledatabase ();
Cursor cursor=db.rawquery ("SELECT *" + "from user where Name=?", New String[]{name.gettext (). toString ()});
User User=null;
if (Cursor.movetofirst ()) {
int id = cursor.getint (cursor.getcolumnindex ("id"));
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
String Password =cursor.getstring (cursor.getcolumnindex ("password"));
User=new User (Id,name,password);
Cursor.close ();
Db.close ();
if (User.getpassword (). Equals (Password.gettext (). toString ())) {
TextView TV = new TextView (this);
Tv.settext (User.tostring ());
Tv.settextsize (20);
Layout.addview (TV);
}
else if (Password.gettext (). toString (). Length () ==0) {
Toast.maketext (mainactivity.this, "user does not exist", toast.length_short). Show ();
}
Else
Toast.maketext (mainactivity.this, "Incorrect password", Toast.length_short). Show ();
}}
Break
}}}
Mydatabasehelper.java
public class Mydatabasehelper extends Sqliteopenhelper {
public static final String Create_user = "CREATE TABLE USER ("///creation of tables named Create_user
+ "ID integer primary key autoincrement,"
+ "Name text NOT NULL,"
+ "password integer not nul L) ";
Private Context Mcontext;
//Four parameters are Context, the database name created, a custom value returned at query time (typically null), the version number of the database
Public mydatabasehelper (context context, String nam E, sqlitedatabase.cursorfactory factory, int version) {
Super (context, name, factory,version);
Mcontext = context;
}
@Override
public void onCreate (Sqlitedatabase db) {
Db.execsql (create_user);//execute this build statement Toast.maketext (Mcontext, "", Toast.length_short). Show ();//toast is the use of a popup prompt
}
@Override
Pu Blic void Onupgrade (sqlitedatabase db,int oldversion,int newversion) {
Db.execsql ("drop table if exists User"); br> onCreate (DB);
}
}
User.java
public class User {
private String name;
private String password;
private int id;
Public User (int id,string name,string password) {
Super ();
This.id = ID;
THIS.name = name;
This.password = password;
}
public void setId (int id) {
This.id = ID;
}
public void SetName (String name) {
THIS.name = name;
}
public void SetPassword (String password) {
This.password = password;
}
public int getId () {
return ID;
}
Public String GetName () {
return name;
}
Public String GetPassword () {
return password;
}
Public String toString () {
Return "Id:" +this.id+ " Name:" +this.name+ " password:" +this.password;
}
}
Use case of SQLite