示例图 :

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;
}
}
												

SQLite的使用案例的更多相关文章

  1. OpenStreetMap数据清洗(SQL&MonogoDB版本)

    目标:通过网上下载的OpenStreetMap.xml数据格式,将该文件的格式进行统计,清洗,并导出成CSV格式的文件,最后倒入到SQLite中 本案例中所需的包 import csv import ...

  2. 掘金 Android 文章精选合集

    掘金 Android 文章精选合集 掘金官方 关注 2017.07.10 16:42* 字数 175276 阅读 50053评论 13喜欢 669 用两张图告诉你,为什么你的 App 会卡顿? - A ...

  3. 【Android自己定义View实战】之自己定义超简单SearchView搜索框

    [Android自己定义View实战]之自己定义超简单SearchView搜索框 这篇文章是对之前文章的翻新,至于为什么我要又一次改动这篇文章?原因例如以下 1.有人举报我抄袭,原文链接:http:/ ...

  4. Android(java)学习笔记195:学生信息管理系统案例(SQLite + ListView)

    1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...

  5. IOS UIPickView+sqlite 选择中国全部城市案例

    1.案例简单介绍 通过读取文件.将中国全部城市写入sqlite数据库中,现通过UIPickView实现中国全部城市的选择,效果图例如以下所看到的 2.城市对象模型 中国全部城市数据请看http://b ...

  6. Android(java)学习笔记188:学生信息管理系统案例(SQLite + ListView)

    1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...

  7. IOS Sqlite用户界面增删改查案例

    1.案例简单介绍 对SQLite操作进行了简单的封装,将对数据表操作转变成对对象的操作,并通过UI界面完毕对用户表的增.删.改.查,执行界面例如以下图所看到的 a 2.项目project文件夹 Use ...

  8. SQLite数据库以及增删改查的案例

    Android使用开源的与操作系统无关的SQL数据库——SQLite 一:在命令行下创建数据库: 1.启动模拟器后,打开命令行,执行adb shell 2.进入所在工程目录 3.执行sqlite3 m ...

  9. Android SQLite案例

    重点掌握execSQL()和rawQuery()方法,rawQuery()方法用于执行select语句. SQLiteOpenHelper,实现了onCreate和onUpgrade方法. 第一次创建 ...

随机推荐

  1. 零元学Expression Blend 4 - Chapter 38 看如何使用Clip修出想要的完美曲线(下)

    原文:零元学Expression Blend 4 - Chapter 38 看如何使用Clip修出想要的完美曲线(下) 你可以把Clip想成是一个遮罩,运用遮罩达到我们想要的效果 所以在这里我们把文字 ...

  2. 危险的DDD聚合根

    原文:危险的DDD聚合根 DDD的核心是聚合.这没有问题,大家都认同.但关于DDD中的聚合方式,其实我还是有些担心,下面说说我的想法,希望大家参与讨论.其实当初第一次看到DDD中关于聚合根部分论述的时 ...

  3. 基于事件驱动的DDD领域驱动设计框架分享(附源代码)

    原文:基于事件驱动的DDD领域驱动设计框架分享(附源代码) 补充:现在再回过头来看这篇文章,感觉当初自己偏激了,呵呵.不过没有以前的我,怎么会有现在的我和现在的enode框架呢?发现自己进步了真好! ...

  4. 为什么需要使用Git客户端?(使用msysgit)

    Git 是 Linux Torvalds 为了帮助管理 Linux® 内核开发而开发的一个开放源码的版本控制软件.正如所提供的文档中说的一样,“Git 是一个快速.可扩展的分布式版本控制系统,它具有极 ...

  5. Wiki上的C++哲学

    Philosophy[edit] Throughout C++'s life, its development and evolution has been informally governed b ...

  6. .NET程序员如何快入门Spring Boot

    本篇文章将教你作为一个.NET程序员如何快入门Spring Boot.你不需要用Eclipse,也不需要用IDEA.已经习惯了VS,其他的IDE-- 但不得不说VS Code很厉害,一用就喜欢.微软给 ...

  7. RocketMQ(2)---Docker集群部署RocketMQ

    RocketMQ(2)-Docker集群部署RocketMQ =前言= 1.因为自己只买了一台阿里云服务器,所以RocketMQ集群都部署在单台服务器上只是端口不同,如果实际开发,可以分别部署在多台服 ...

  8. Java连载3-编译与运行阶段详解&JRE,JDK,JVM关系

    ·一. 1.JDK下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html ...

  9. 彻底弄懂UTF-8、Unicode、宽字符、locale

    目录 Unicode.UCS UTF8 宽字符类型wchar_t locale 为什么需要宽字符类型 多字节字符串和宽字符串相互转换 最近使用到了wchar_t类型,所以准备详细探究下,没想到水还挺深 ...

  10. GIT \ SVN 版本管理 git + gitHub

    场景1   想删除一个段落,又怕将来想恢复找不回来怎么办?有办法,先把当前文件"另存为--"一个新的Word文件,再接着改,改到一定程度,再"另存为--"一个新 ...