1. 示例图

activity_main.xml :

  1. <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:"/>
  2.  
  3. <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="注册"
  4.  
  5. />
  6.  
  7. <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="删除"
  8.  
  9. />
    <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="查询"
  10.  
  11. />
    <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>
  12.  
  13. MainActivity.java :
  1. 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>();
  2.  
  3. }
    public void onClick(View view){
    switch (view.getId()) {
  4.  
  5. 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();
  6.  
  7. } else
    Toast.makeText(MainActivity.this, "输入不合法", Toast.LENGTH_SHORT).show();
    break;
  8.  
  9. 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();
  10.  
  11. 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();
  12.  
  13. } else
    Toast.makeText(MainActivity.this, "密码不正确", Toast.LENGTH_SHORT).show();
  14.  
  15. 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();
  16.  
  17. } else
    Toast.makeText(MainActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
  18.  
  19. db.close();
    }
  20.  
  21. }
    break;
  22.  
  23. case R.id.btn_sel:
  24.  
  25. Layout.removeAllViews();
  26.  
  27. 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);
  28.  
  29. 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();
  30.  
  31. } else if (Name.getText().toString().length() != 0 && Password.getText().toString().length() == 0) {
  32.  
  33. 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;
  34.  
  35. 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();
  36.  
  37. TextView tv = new TextView(this);
    tv.setText(user.toString());
    tv.setTextSize(20);
    Layout.addView(tv);
    }
    }
  38.  
  39. 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;
  40.  
  41. 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();
  42.  
  43. 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();
    }
  44.  
  45. else
    Toast.makeText(MainActivity.this,"密码不正确",Toast.LENGTH_SHORT).show();
    }}
    break;
    }}}
  1. MyDatabaseHelper.java

    public class MyDatabaseHelper extends SQLiteOpenHelper {
  1. 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);
    }
    }
  1. User.java
  1. public class User {
    private String name;
    private String password;
    private int id;
  2.  
  3. 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;
    }
    }
  4.  
  1.  

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. VS2013环境里安装QT插件-“X86”与目标计算机类型“x64”冲突

    在VS2013环境里搭载QT老是出现模块计算机类型“X86”与目标计算机类型“x64”冲突 2.解决方案2.1 项目右键,属性>配置管理选择>x64,没有的话新建:2.2  项目右键,属性 ...

  2. HTTP请求(GET与POST区别)和响应(get是从服务器上获取数据,post是向服务器传送数据,格式与举例都非常清楚)

    HTTP有两部分组成:请求与响应,下面分别整理. 一.HTTP请求 1.HTTP请求格式: <request line> <headers> <blank line> ...

  3. 零元学Expression Blend 4 - Chapter 47 超简单!运用StackPanel配合OpacityMask做出倒影效果

    原文:零元学Expression Blend 4 - Chapter 47 超简单!运用StackPanel配合OpacityMask做出倒影效果 有网友问我如何在Blend内制作出倒影效果 我提供了 ...

  4. C语言中.h和.c文件解析(转载)

    转载:http://www.cnblogs.com/laojie4321/archive/2012/03/30/2425015.html   简单的说其实要理解C文件与头文件(即.h)有什么不同之处, ...

  5. ORACLE RAC+OGG配置

    实验环境主机名   IP地址rac01     192.168.56.10rac02     192.168.56.20rac-scan 192.168.56.30 目标库:ora-ogg  192. ...

  6. 12 寸 Retina MacBook 的大秘密: 可用移动电源充电

    苹果新款12寸Retina MacBook虽然只有一个USB-C接口,但这个接口的能力却十分强大.它不仅可以进行数据传输和视频输出,还能接收和输入电源.这也就是说,你可以使用移动电源对其进行充电,如果 ...

  7. 去除文件属性(使用SetFileAttributes API函数)

    FILE_ATTRIBUTE_ARCHIVE 文件存档(备份或移动时会对文件做标记).FILE_ATTRIBUTE_ENCRYPTED 加密(对文件来说是内容加密,对目录来说是对将来新建的文件默认为加 ...

  8. linux环境下使用百度云网盘

    linux下经常需要备份一些文件到云端,现在能用的也就只有度娘的百度云网盘了,在github上发现一个挺好的项目,bypy,用来在linux下使用百度云. 项目地址:https://github.co ...

  9. SAP TABLECONTROL 自定义SEARCH HELP

    项目上需要开发一个界面如下的应用程序.这是一个MB1A发料的辅助程序,限制住移动类型和在特定字段写入产品号. 这个应用程序的主要功能毫无疑问是通过BAPI实现的.但在TABLECONTROL中需要对填 ...

  10. vmware linux虚拟机忘记密码怎么办

    你又忘了root密码??? 针对好多好多同学经常忘记root密码...这里给你整理怎么重置root密码!! 重启 Linux 系统主机并出现引导界面时,按下键盘上的 e 键进入内核编辑界面 在 lin ...