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;
    }
}
SQLite的使用案例的更多相关文章
- OpenStreetMap数据清洗(SQL&MonogoDB版本)
		
目标:通过网上下载的OpenStreetMap.xml数据格式,将该文件的格式进行统计,清洗,并导出成CSV格式的文件,最后倒入到SQLite中 本案例中所需的包 import csv import ...
 - 掘金 Android 文章精选合集
		
掘金 Android 文章精选合集 掘金官方 关注 2017.07.10 16:42* 字数 175276 阅读 50053评论 13喜欢 669 用两张图告诉你,为什么你的 App 会卡顿? - A ...
 - 【Android自己定义View实战】之自己定义超简单SearchView搜索框
		
[Android自己定义View实战]之自己定义超简单SearchView搜索框 这篇文章是对之前文章的翻新,至于为什么我要又一次改动这篇文章?原因例如以下 1.有人举报我抄袭,原文链接:http:/ ...
 - Android(java)学习笔记195:学生信息管理系统案例(SQLite + ListView)
		
1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...
 - IOS UIPickView+sqlite 选择中国全部城市案例
		
1.案例简单介绍 通过读取文件.将中国全部城市写入sqlite数据库中,现通过UIPickView实现中国全部城市的选择,效果图例如以下所看到的 2.城市对象模型 中国全部城市数据请看http://b ...
 - Android(java)学习笔记188:学生信息管理系统案例(SQLite + ListView)
		
1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...
 - IOS Sqlite用户界面增删改查案例
		
1.案例简单介绍 对SQLite操作进行了简单的封装,将对数据表操作转变成对对象的操作,并通过UI界面完毕对用户表的增.删.改.查,执行界面例如以下图所看到的 a 2.项目project文件夹 Use ...
 - SQLite数据库以及增删改查的案例
		
Android使用开源的与操作系统无关的SQL数据库——SQLite 一:在命令行下创建数据库: 1.启动模拟器后,打开命令行,执行adb shell 2.进入所在工程目录 3.执行sqlite3 m ...
 - Android SQLite案例
		
重点掌握execSQL()和rawQuery()方法,rawQuery()方法用于执行select语句. SQLiteOpenHelper,实现了onCreate和onUpgrade方法. 第一次创建 ...
 
随机推荐
- Win8 Metro(C#)数字图像处理--2.47人脸红眼去除算法
			
原文:Win8 Metro(C#)数字图像处理--2.47人脸红眼去除算法  [函数名称] 红眼去除 RedeyeRemoveProcess(WriteableBitmap src) ...
 - Android adb socket 连接失败的问题
			
pc客户端通过adb forward tcp 与android app通信 场景:pc启动,能正常建立连接,当断开连接再次连接,偶现pc客户端无法与forward 的端口建立 socket连接,连接请 ...
 - EasyUI 实现编辑功能,给Combobox  赋值
			
1: <input id="RequestType" name="RequestType" class="easyui-combobox&quo ...
 - Android零基础入门第84节:引入Fragment原来是这么回事
			
随着大众生活水平的提高,再加上移动互联网的迅速发展,几乎每个人都至少拥有一台搭载Android系统的移动设备.Android设备的多样性给我们带来了很大的便捷,各Android设备拥有不同分辨率和不同 ...
 - Android零基础入门第75节:Activity状态和生命周期方法
			
前面两期我们学习了Activity的创建和注册.以及启动和关闭,也学会了重写onCraete方法,这些知识在实际开发中远远不够,还需要学习了解更多. 生命周期就是一个对象从创建到销毁的过程,每一个对象 ...
 - Model1简介
			
Model1模型出现前,整个Web应用的情况:几乎全部由JSP页面组成,JSP页面接收处理客户端请求,对请求处理后直接做出响应. 弊端:在界面层充斥着大量的业务逻辑的代码和数据访问层的代码,Web程序 ...
 - ASP.NET vNext 微笔记
			
关心 ASP.NET vNext 的人可能已经读过相关文章,例如:ASP.NET vNext @ 2014.那么,你可能已经知道,ASP.NET vNext 摆脱了 System.Web.DLL,把 ...
 - 304902阿里巴巴Java开发手册1.4.0
			
转自官网 前言 <阿里巴巴Java开发手册>是阿里巴巴集团技术团队的集体智慧结晶和经验总结,经历了多次大规模一线实战的检验及不断完善,系统化地整理成册,回馈给广大开发者.现代软件行业的高速 ...
 - Exceptionless(二) - 使用进阶
			
Exceptionless(二) - 使用进阶 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/11100563.html 官网 ...
 - mysql5.7 group by语法 1055
			
先来看如下语句,查询默认存在的引擎表 之前使用的MySQL版本为5.7以下,根据support进行分组执行语句如下 添加跟分组support无关的字段engine 没有任何问题 现在使用的版本是5.7 ...