SQLite的使用案例

示例图 :

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="注册"

/>

<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="删除"

/>
<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="查询"

/>
<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() < 13 && 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, "注册成功", Toast.LENGTH_SHORT).show();

} else
Toast.makeText(MainActivity.this, "输入不合法", 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, "删除成功", Toast.LENGTH_SHORT).show();

} else
Toast.makeText(MainActivity.this, "密码不正确", 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, "删除成功", Toast.LENGTH_SHORT).show();

} else
Toast.makeText(MainActivity.this, "用户不存在", 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,"用户不存在",Toast.LENGTH_SHORT).show();
}

else
Toast.makeText(MainActivity.this,"密码不正确",Toast.LENGTH_SHORT).show();
}}
break;
}}}

MyDatabaseHelper.java

public class MyDatabaseHelper extends SQLiteOpenHelper {
    public static final String CREATE_USER = "create table User("
+"id integer primary key autoincrement,"
+"name text not null,"
+"password integer not null)";
private Context mContext;
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context , name ,factory,version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_USER);
Toast.makeText(mContext, "", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
db.execSQL("drop table if exists User");
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+" 姓名:"+this.name+" 密码:"+this.password;
}
}






原文地址:https://www.cnblogs.com/yunting/p/8527540.html