zoukankan      html  css  js  c++  java
  • 1-SI--安卓SQLite基础使用指南

    零、前言

    [1]熟悉MySQL的学这个就像会西瓜的人去学吃哈密瓜一样简单。
    [2]如果对MySQL不太熟悉的童鞋,可以看一下我的这篇:SpringBoot-14-MyBatis预热篇,MySQL小结
    [3]SQLite:安卓内置轻量级的关系型数据库
    [4]强烈建议语句什么的提前写好,在MySQL上测试一下,不然少个分号,多个逗号什么的就呵呵了
    [5]安卓有API支持数据库操作,但感觉不怎么灵活,感兴趣的可以自己了解一下
    [6]本篇介绍基础使用,下篇会封装一下。

    坑点

    [1]:SQLite 不支持 DEFAULT 关键字
    [2]:INSERT INTO 的 INTO 要加上 (MySQL养成的坏毛病,得该)


    一、创建数据库

    1.SQL常量类:SQLCon.java
    /**
     * 作者:张风捷特烈<br/>
     * 时间:2018/8/26 0026:14:48<br/>
     * 邮箱:1981462002@qq.com<br/>
     * 说明:SQL常量类
     */
    public class SQLCon {
        /**
         * 数据库名
         */
        public static String DB_NAME = "weapon";
    
        /**
         * 数据库版本
         */
        public static int DB_VERSION = 1;
    
        /**
         * 建表语句
         */
        public static final String CREATE_TABLE = "CREATE TABLE sword (
    " +
                "id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    " +
                "name VARCHAR(32) NOT NULL,
    " +
                "atk SMALLINT UNSIGNED NOT NULL,
    " +
                "hit SMALLINT UNSIGNED NOT NULL DEFAULT 20,
    " +
                "crit SMALLINT UNSIGNED NOT NULL DEFAULT 10
    " +
                ");";
        
    }
    
    
    2.SQLiteOpenHelper使用:我的数据库辅助类
    /**
     * 作者:张风捷特烈<br/>
     * 时间:2018/8/26 0026:14:26<br/>
     * 邮箱:1981462002@qq.com<br/>
     * 说明:我的数据库辅助类
     */
    public class MySQLHelper extends SQLiteOpenHelper {
    
        private Context mContext;
    
        /**
         * 构造函数
         *
         * @param context 上下文
         */
        public MySQLHelper(Context context) {
            super(context, SQLCon.DB_NAME, null, SQLCon.DB_VERSION);
            mContext = context;
        }
    
        /**
         * 创建数据库,数据库存在就不会执行
         *
         * @param db SQLite数据库对象
         */
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(SQLCon.CREATE_TABLE);//创建表
        }
    
        /**
         * 数据库进行升级
         *
         * @param db         SQLite数据库对象
         * @param oldVersion 旧版本
         * @param newVersion 新版本
         */
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            
        }
    }
    
    3.在需要的地方使用:
    MySQLHelper mySQLHelper = new MySQLHelper(this);//创建辅助对象
    mySQLHelper.getWritableDatabase();//获取可写数据库对象
    
    //getReadableDatabase()和getWritableDatabase()
    //这两个方法都可以创建或打开一个现有的数据库,并返回一个可对数据库进行读写操作的对象。
    //磁盘空间已满时getWritableDatabase()异常
    

    9414344-ca17ddc3873e3da1.png
    创建数据库.png

    二、升级数据库时删除表

    1.SQL常量类,将数据库版本改到2:SQLCon.java
        /**
         * 数据库版本
         */
        public static int DB_VERSION = 2;
        
        /**
         * 删除表语句
         */
        public static final String DROP_TABLE = "DROP TABLE sword";
    
    2.com.toly1994.si_sqlite.MySQLHelper#onUpgrade
    db.execSQL(SQLCon.DROP_TABLE);
    L.d(oldVersion+":"+newVersion+L.l());//1:2
    
    3.在需要的地方使用
    MySQLHelper mySQLHelper2 = new MySQLHelper(this);//创建辅助对象
    mySQLHelper2.getWritableDatabase();//获取可写数据库对象
    

    三、插入数据

    1.SQL常量类
        /**
         * 插入语句
         */
        public static final String INSERT = "INSERT INTO sword(id,name,atk,hit,crit) VALUES" +
                "(1,'痕兮',7000,800,999)," +
                "(2,'逐暮',100,1000,10000)," +
                "(3,'风跃',9000,10,255);";
    
    2..在需要的地方使用
    mDb = new MySQLHelper(this).getWritableDatabase();
    mDb.execSQL(SQLCon.INSERT);
    
    9414344-6759e86086cfc64a.png
    插入数据.png

    四、删除数据

    1.SQL常量类
        /**
         * 删除数据
         */
        public static final String DELETE = "DELETE FROM sword WHERE id=1;";
    
    2.在需要的地方使用
    mDb = new MySQLHelper(this).getWritableDatabase();
    mDb.execSQL(SQLCon.DELETE);
    
    9414344-6b1482c5bbc39d57.png
    删除数据.png

    五、修改数据

    1.SQL常量类
        /**
         * 修改数据
         */
        public static final String UPDATE = "UPDATE sword SET hit=hit+1;";
    
    2.在需要的地方使用
    mDb = new MySQLHelper(this).getWritableDatabase();
    mDb.execSQL(SQLCon.UPDATE);
    
    9414344-8b9c8d9e8ec8676b.png
    修改数据.png

    五、查询数据

    1.查询所有
    Cursor cursor = mDb.rawQuery("SELECT * FROM sword", null);
    while (cursor.moveToNext()) {
        String id = cursor.getString(cursor.getColumnIndex("id"));
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String atk = cursor.getString(cursor.getColumnIndex("atk"));
        String hit = cursor.getString(cursor.getColumnIndex("hit"));
        String crit = cursor.getString(cursor.getColumnIndex("crit"));
        System.out.println(id + "---" + name + "---" + atk + "---" + hit + "---" + crit);
    }
        //2---逐暮---100---1001---10000
        //3---风跃---9000---11---255
    cursor.close();//关闭游标
    
    2.查询一个:?为占位符,后面String数组对应站位符位置,占位符可多个。
     Cursor cursor2 = mDb.rawQuery("SELECT * FROM sword WHERE id = ?", new String[]{"2"});
    while (cursor2.moveToNext()) {
        String id = cursor2.getString(cursor2.getColumnIndex("id"));
        String name = cursor2.getString(cursor2.getColumnIndex("name"));
        String atk = cursor2.getString(cursor2.getColumnIndex("atk"));
        String hit = cursor2.getString(cursor2.getColumnIndex("hit"));
        String crit = cursor2.getString(cursor2.getColumnIndex("crit"));
        System.out.println(id + "---" + name + "---" + atk + "---" + hit + "---" + crit);
    }
    //2---逐暮---100---1001---10000
    cursor2.close();//关闭游标
    

    后记、

    1.声明:

    [1]本文由张风捷特烈原创,转载请注明
    [2]欢迎广大编程爱好者共同交流
    [3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
    [4]你的喜欢与支持将是我最大的动力

    2.连接传送门:

    更多安卓技术欢迎访问:安卓技术栈
    我的github地址:欢迎star
    简书首发,腾讯云+社区同步更新
    张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com

    3.联系我

    QQ:1981462002
    邮箱:1981462002@qq.com
    微信:zdl1994328

    4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
    9414344-c474349cd3bd4b82.jpg
    公众号.jpg
  • 相关阅读:
    ios初级必看视频
    Md5加密
    Jmail发送邮件
    NPOI Helper文档
    jquery 序列化
    mvc DropDownList默认选项
    获取HTML
    EntityFramework Reverse POCO Generator工具
    全选反选
    mvc导出EXCEL
  • 原文地址:https://www.cnblogs.com/toly-top/p/9781936.html
Copyright © 2011-2022 走看看