zoukankan      html  css  js  c++  java
  • Android-Sqlite-SQL操作增删改查

    一想到Android到数据库,只需要想到一个类 SQLiteOpenHelper,然后写一个类继承 SQLiteOpenHelper,重写构造方法,对数据库进行配置

    public class MySQLiteOpenHelper extends SQLiteOpenHelper {
      public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
          super(context, name, factory, version);
      }
    }

    SqliteDatabase数据库,所写的所有SQL语句都是通过系统sqlite.c文件执行成数据库文件的,SQlite数据库是关系型数据库,轻量级,体积小等特点

    实现MySQLiteOpenHelper数据库帮助类:

    package liudeli.datastorage.db;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    
        public static MySQLiteOpenHelper mySQLiteOpenHelper;
    
        private static final String DB_NAME = "person_info.db";
        private static final int VERSION = 1;
    
        public synchronized static MySQLiteOpenHelper getInstance(Context context) {
            if (null == mySQLiteOpenHelper) {
                mySQLiteOpenHelper = new MySQLiteOpenHelper(context, DB_NAME, null, VERSION);
            }
            return mySQLiteOpenHelper;
        }
    
        /**
         * 当开发者调用 getReadableDatabase(); 或者 getWritableDatabase();
         * 就会通过此构造方法配置的信息 来创建 person_info.db 数据库
         * 此方法的另外作用是,如果存着数据库就打开数据库,不存着数据库就创建数据库
         * @param context 上下文
         * @param name    数据库名
         * @param factory 游标工厂
         * @param version 版本,最低为1
         */
        private MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }
    
        /**
         * 此方法是何时调用? ,是需要开发者调用 getReadableDatabase(); 或者 getWritableDatabase();
         * 此方法的作用是,如果没有表就创建打开,如果有表就打开
         * @param db 可执行SQL语句
         */
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table student_table(_id integer primary key autoincrement, name text, age integer);");
        }
    
        /**
         * 此方法用于数据库升级
         * @param db 可执行SQL语句
         * @param oldVersion 以前旧版本的版本号
         * @param newVersion 现在目前最新的版本号
         */
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
        }
    }

    写SQL语句进行增删改查操作:

    package liudeli.datastorage;
    
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    import liudeli.datastorage.db.MySQLiteOpenHelper;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private final String TAG = MainActivity.class.getSimpleName();
    
        private MySQLiteOpenHelper mySQLiteOpenHelper;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            mySQLiteOpenHelper = MySQLiteOpenHelper.getInstance(this);
    
            // person_info.db创建成功  table表创建成功  还有一个很重要的作用:刷新数据库 刷新表数据等
            mySQLiteOpenHelper.getReadableDatabase();
            mySQLiteOpenHelper.getWritableDatabase();
    
            initViewListener();
        }
    
        private void initViewListener() {
            Button btQuery = findViewById(R.id.bt_query);
            Button btInsert = findViewById(R.id.bt_insert);
            Button btUpdate = findViewById(R.id.bt_update);
            Button btDelete = findViewById(R.id.bt_delete);
    
            btQuery.setOnClickListener(this);
            btInsert.setOnClickListener(this);
            btUpdate.setOnClickListener(this);
            btDelete.setOnClickListener(this);
        }
    
        @Override
        protected void onStart() {
            super.onStart();
    
        }
    
        /**
         * 虽然之需要一次 getWritableDatabase getReadableDatabase 就可以来,为什么还需要每次点击都执行一次?
         * 答:getWritableDatabase getReadableDatabase 还有一个很重要的作用:刷新数据库 刷新表数据等
         * @param v
         */
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bt_query: {
                    SQLiteDatabase db = mySQLiteOpenHelper.getReadableDatabase();
                    Cursor cursor = db.rawQuery("select * from "+MySQLiteOpenHelper.TABLE_NAME+";", null);
                    while (cursor.moveToNext()) {
                        int _id = cursor.getInt(cursor.getColumnIndex("_id"));
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        int age = cursor.getInt(cursor.getColumnIndex("age"));
                        Log.d(TAG, "_id:" + _id + " name:" + name + " age:" + age);
                    }
                    db.close();
                    break;
                }
                case R.id.bt_insert: {
                    SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
                    db.execSQL("insert into " + MySQLiteOpenHelper.TABLE_NAME + "(name,age) values('刘德利',19);");
                    db.close();
                    break;
                }
                case R.id.bt_update: {
                    SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
                    db.execSQL("update student_table set name='德利' where _id = 1;");
                    db.close();
                    break;
                }
                case R.id.bt_delete: {
                    SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
                    db.execSQL("delete from student_table where _id = 1;");
                    db.close();
                    break;
                }
                default:
                    break;
            }
        }
    }

    布局代码:

    <?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/bt_query"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查询"
            />
    
        <Button
            android:id="@+id/bt_insert"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="插入"
            />
    
        <Button
            android:id="@+id/bt_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="修改"
            />
    
        <Button
            android:id="@+id/bt_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除"
            />
    
    
    </LinearLayout>
  • 相关阅读:
    团队项目-第一阶段冲刺7
    团队项目-第一阶段冲刺6
    Spring Boot 揭秘与实战(七) 实用技术篇
    Spring Boot 揭秘与实战(七) 实用技术篇
    Spring Boot 揭秘与实战(六) 消息队列篇
    Spring Boot 揭秘与实战(五) 服务器篇
    Spring Boot 揭秘与实战(五) 服务器篇
    Spring Boot 揭秘与实战(五) 服务器篇
    Spring Boot 揭秘与实战(五) 服务器篇
    Spring Boot 揭秘与实战(四) 配置文件篇
  • 原文地址:https://www.cnblogs.com/android-deli/p/10085169.html
Copyright © 2011-2022 走看看