数据库
首页 > 数据库> > Sqltie数据库

Sqltie数据库

作者:互联网

Sqltie数据库

Sqltie数据库介绍

Sqltie数据库是Android 系统中集成的轻量级的数据库
Sqltie数据库特点:

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

它与Mysql的语法一直同时sql语句,并且数据类型也一样

Sqltie数据库的创建

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

一、首先需要创建一个数据库

public class MySqlHelper extends SQLiteOpenHelper {
    /**
     * 构造
     * @param context 上下文
     * @param name  数据库名称
     * @param factory  创建数据的工厂对象
     * @param version 数据版本号
     */
    public MySqlHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    /**
     * 注意:onCreate方法只会执行一次,如果想要修改表的字段,
     * 修改oncreate方法的话是不行的,需要删除之前生成的数据库
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        //primary key autoincrement主键自增
        db.execSQL("create table student (id integer primary key autoincrement, name varchar(20))");
        db.execSQL("insert into student (id,name) values(null,'zhao')");
    }

    /**
     * 更新数据 --- 如果数据库的 版本号发生变化, 执行该方法 --- 执行多次
     * @param db
     * @param oldVersion  老版本号
     * @param newVersion  新版本号
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

执行代码会再data中自动生成一个数据库
在这里插入图片描述

二、需要在MainActivity中创建一个数据库操作类用来执行增、删、改、查的命令

//数据库操作类
private SQLiteDatabase readableDatabase;
//声明数据库
private MySqlHelper mySqlHelper;

//打开数据库
mySqlHelper = new MySqlHelper(this,"user.db",null,1);
//读取本地的数据库
readableDatabase = mySqlHelper.getReadableDatabase();

Sqltie数据库的增删改查

查询数据

btnSelect.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String sql = "select * from student";
        Cursor cursor = readableDatabase.rawQuery(sql,new String[]{});
        //判断数据为空
        if (cursor!=null){
            //移动游标
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                Toast.makeText(MainActivity.this, id + "---" + name, Toast.LENGTH_SHORT).show();
            }
        }
        //关闭游标
        cursor.close();
    }
});

安卓提供的方法:

query(table,columns, selection, selectionArgs, groupBy, having, orderBy, limit)方法各参数的含义:
table:表名。相当于select语句from关键字后面的部分。如果是多表联合查询,可以用逗号将两个表名分开。
columns:要查询出来的列名。相当于select语句select关键字后面的部分。
selection:查询条件子句,相当于select语句where关键字后面的部分,在条件子句允许使用占位符“?”
selectionArgs:对应于selection语句中占位符的值,值在数组中的位置与占位符在语句中的位置必须一致,否则就会有异常。
groupBy:相当于select语句group by关键字后面的部分
having:相当于select语句having关键字后面的部分
orderBy:相当于select语句order by关键字后面的部分,如:personid desc, age asc;
limit:指定偏移量和获取的记录数,相当于select语句limit关键字后面的部分。
 Cursor cursor = readableDatabase.query("student", null, null, null, null, null, null);
 if(cursor != null){
    while (cursor.moveToNext()){
          String id = cursor.getString(cursor.getColumnIndex("id"));
          String name = cursor.getString(cursor.getColumnIndex("name"));
          Toast.makeText(MainActivity.this, id+"-"+name, Toast.LENGTH_SHORT).show();
    }
 }
    cursor.close();

增加数据

btnInsert.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String sql = "insert into student (id, name) values(null,'xu')";
        readableDatabase.execSQL(sql);
    }
});

安卓提供的方法:

ContentValues contentValues = new ContentValues();
contentValues.put("name","xu2");
readableDatabase.insert("student",null,contentValues);

更新数据

btnUpdata.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String sql = "update student set name = ? where id = ?";
        readableDatabase.execSQL(sql,new Object[]{"hui","1"});
    }
});

安卓提供的方法:

 ContentValues contentValues = new ContentValues();
                contentValues.put("name","hui");
                readableDatabase.update("student",contentValues,"id = ?",new String[]{"1"});

删除数据

btnDelete.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String sql = "delete from student where id=1";
        readableDatabase.execSQL(sql);
    }
});

安卓提供的方法:

 //2 系统提供
readableDatabase.delete("student","id = ?",new String[]{"2"});

案例整体代码

public class MainActivity extends AppCompatActivity {
    private Button btnSelect;
    private Button btnInsert;
    private Button btnDelete;
    private Button btnUpdata;

    //数据库操作类
    private SQLiteDatabase readableDatabase;
    //声明数据库
    private MySqlHelper mySqlHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSelect = (Button) findViewById(R.id.btn_select);
        btnInsert = (Button) findViewById(R.id.btn_insert);
        btnDelete = (Button) findViewById(R.id.btn_delete);
        btnUpdata = (Button) findViewById(R.id.btn_updata);

        //打开数据库
        mySqlHelper = new MySqlHelper(this,"user.db",null,1);
        //读取本地的数据库
        readableDatabase = mySqlHelper.getReadableDatabase();

        btnUpdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql = "update student set name = ? where id = ?";
                readableDatabase.execSQL(sql,new Object[]{"hui","1"});
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql = "delete from student where id=1";
                readableDatabase.execSQL(sql);
            }
        });

        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql = "insert into student (id, name) values(null,'xu')";
                readableDatabase.execSQL(sql);
            }
        });



        btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql = "select * from student";
                Cursor cursor = readableDatabase.rawQuery(sql,new String[]{});
                //判断数据为空
                if (cursor!=null){
                    while (cursor.moveToNext()) {
                        int id = cursor.getInt(cursor.getColumnIndex("id"));
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        Toast.makeText(MainActivity.this, id + "---" + name, Toast.LENGTH_SHORT).show();
                    }
                }
                //关闭游标
                cursor.close();
            }
        });
    }
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询" />

    <Button
        android:id="@+id/btn_insert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="插入" />

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除" />

    <Button
        android:id="@+id/btn_updata"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更新" />


</LinearLayout>
汪星没有熊 发布了10 篇原创文章 · 获赞 0 · 访问量 2399 私信 关注

标签:cursor,name,数据库,Sqltie,new,id,String
来源: https://blog.csdn.net/weixin_45697390/article/details/104615140