美文网首页
SQLite数据库

SQLite数据库

作者: 鴻9527 | 来源:发表于2019-08-12 19:39 被阅读0次

    SQLite数据库介绍

    • Android系统中集成的轻量级的数据库

    • 特点 : 轻量级;只用一个动态的库;是以单个文件的形式进行存取;跨平台;支持多个操作系统;零配置;无需安装;直接使用;嵌入式,内嵌到手机中

    • 在程序的内部可以通过数据库的名称访问, 其他应用不能访问

    • 路径: data/data/应用程序包名/database/ * * * *.db

    • 存放的类型:

      NULL 空值
      INTEGER 整型(不用int)
      VARCHAR 可变长度的字符数据
      TEXT 文本字符串
      BOOLEAN 布尔
      DATE
      TIME

    SQLiteOpenHelper 数据库的帮助类 --> 代码实现步骤

    1. 定义一个类 继承SQLiteOpenHelper
    2. 重写构造方法 :提供数据库的基本信息 : 上下文对象,数据库名称,Null,数据库的版本号
    3. 重写父类的方法onCreate() 和onUpgrade()

    XML文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/button_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="插入数据"
            android:textSize="25dp"
            android:layout_centerHorizontal="true"/>
    
        <Button
            android:id="@+id/button_updata"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="更新数据"
            android:textSize="25dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/button_add"
            android:layout_marginTop="20dp"/>
    
        <Button
            android:id="@+id/button_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除数据"
            android:textSize="25dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/button_updata"
            android:layout_marginTop="20dp"/>
    
        <Button
            android:id="@+id/button_query"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查询数据"
            android:textSize="25dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/button_delete"
            android:layout_marginTop="20dp"/>
    
    </RelativeLayout>
    

    JAVA代码

    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class MySQLite extends SQLiteOpenHelper {
        private Context context;
        private String name;
        private SQLiteDatabase.CursorFactory factory;
        private int version;
    
    
        /***
         *
         * @param context 上下文
         * @param name 数据库名
         * @param factory
         * @param version 版本号
         */
        public MySQLite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
            this.context = context;
            this.name = name;
            this.factory = factory;
            this.version = version;
        }
    
        //当传进来的数据库名没找到时 创建该数据库 并执行该方法
        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            //随便创建一张表
            String sql = "create table user(_id integer primary key autoincrement,Name varchar(20),Age integer)";
            sqLiteDatabase.execSQL(sql);
        }
    
        //更新版本时执行
        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    
        }
    }
    

    User类

    package com.example.day09_03_sql;
    
    public class User {
    
        private Integer _id;
        private String name;
    
        public User() {
        }
    
        public User(Integer _id, String name) {
            this._id = _id;
            this.name = name;
        }
    
        public Integer get_id() {
            return _id;
        }
    
        public void set_id(Integer _id) {
            this._id = _id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "_id=" + _id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    

    添加数据的两种方法

    //按钮点击事件
    button_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //第一种方法
                    String sql = "insert into user(_id,name) values (null,'白')";
                    db.execSQL(sql);
    
                    //第一种方法改进
                    String sql1 = "insert into user values (null,?)";
                    db.execSQL(sql1,new Object[]{"秀"});
    
                    //第二种方法
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("name","香");
    
                    db.insert("user",null,contentValues);
    
                }
            });
    

    更新数据的两种方法

    //更新数据
            button_uodata.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //第一种方法
                    String sql = "update user set name = ? where _id = ?";
                    db.execSQL(sql,new Object[]{"劫",1});
    
                    //第二种方法
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("name","劫");
                    db.update("user",contentValues,"_id = ?",new String[]{"2"});
    
                }
            });
    

    删除数据的两种方法

    //删除数据
            button_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //第一种方法
                    String sql = "delete from user where _id = ?";
                    db.execSQL(sql,new Object[]{"5"});
                    //第二种数据
                    db.delete("user","_id = ?",new String[]{"6"});
                }
            });
    

    查询数据的两种方法

     //查询数据
            button_query.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    //第一种方法
                    String sql = "select * from user where _id = ?";
                    Cursor cursor = db.rawQuery(sql,new String[]{"16"});
                    if (cursor != null){
                        while (cursor.moveToNext()){
                            String name = cursor.getString(cursor.getColumnIndex("name"));
                            int id = cursor.getInt(cursor.getColumnIndex("_id"));
                            Toast.makeText(MainActivity.this,id + name, Toast.LENGTH_SHORT).show();
                        }
                    }
                    cursor.close();
    
                    //第二种方法
                    Cursor cursor = db.query("user", null, null, null, null, null, null);
                    if (cursor != null){
                        while (cursor.moveToNext()){
                            User user = new User();
                            String name = cursor.getString(cursor.getColumnIndex("name"));
                            int id = cursor.getInt(cursor.getColumnIndex("_id"));
    //                        Toast.makeText(MainActivity.this, id + name, Toast.LENGTH_SHORT).show();
                            user.setName(name);
                            user.set_id(id);
                            users.add(user);
                            Toast.makeText(MainActivity.this, user.getName() + user.get_id() + "长度" + users.size(), Toast.LENGTH_SHORT).show();
                        }
                    }
                    cursor.close();
                }
            });
    

    相关文章

      网友评论

          本文标题:SQLite数据库

          本文链接:https://www.haomeiwen.com/subject/uydpjctx.html