SQL学习实践demo

学习目标

了解sql的基本信息及用法,应用到实际项目中

SQL的基本信息

SQL 指结构化查询语言,全称是 Structured Query Language。
SQL 让您可以访问和处理数据库,包括数据插入、查询、更新和删除。
SQL 在1986年成为 ANSI(American National Standards Institute 美国国家标准化组织)的一项标准,在 1987 年成为	国际标准化组织(ISO)标准。

SQL的资源信息

1.学习网站:菜鸟教程 https://www.runoob.com/sql/sql-intro.html   
2.学习资源链接:https://github.com/ravi8x/AndroidSQLite

SQL的基本语法描述

1.SQL INSERT INTO 语法

	第一种形式无需指定要插入数据的列名,只需提供被插入的值即可:
	INSERT INTO table_name VALUES (value1,value2,value3,...);

	第二种形式需要指定列名及被插入的值:
	INSERT INTO table_name  (column1,column2,column3,...)  VALUES (value1,value2,value3,...);

2.SQL UPDATE 语法

UPDATE table_name  SET column1=value1,column2=value2,...  WHERE some_column=some_value;

3.SQL DELETE 语法

DELETE FROM table_name  WHERE some_column=some_value;

4.SQL SELECT 语法

SELECT column_name,column_name  FROM table_name;
SELECT * FROM table_name;
例 SELECT * FROM Websites WHERE country='CN';  

SQL的实际使用

1.Note.java —表格类型确定

加入新的type,用于和id区分利于处理 加入里插入图片描述
加入

2.DatabaseHelper.java -------增删改查调用

    public long insertNote(String note,String type) {       
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();  
        values.put(Note.COLUMN_NOTE, note);
        values.put(Note.COLUMN_TYPE, type);      
        long id = db.insert(Note.TABLE_NAME, null, values);       
        db.close();       
        return id;
    }

    public void deleteNote(Note note) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(Note.TABLE_NAME, Note.COLUMN_ID + " = ?",
                new String[]{String.valueOf(note.getId())});
        db.close();
    }

    public void updateType(String note,String type) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(Note.COLUMN_NOTE, note);      
         db.update(Note.TABLE_NAME, values, Note.COLUMN_TYPE + " = ?",
                new String[]{type});
    }

    public List<Note> getAllNotes() {
        List<Note> notes = new ArrayList<>();       
        String selectQuery = "SELECT  * FROM " + Note.TABLE_NAME + " ORDER BY " +
                Note.COLUMN_TIMESTAMP + " DESC";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);       
        if (cursor.moveToFirst()) {
            do {
                Note note = new Note();
                note.setId(cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)));
                note.setType(cursor.getString(cursor.getColumnIndex(Note.COLUMN_TYPE)));
                note.setNote(cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)));
                note.setTimestamp(cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
                notes.add(note);
            } while (cursor.moveToNext());
        }       
        db.close();       
        return notes;
    }

通过TYPE进行类型查询

    public List<Note> getAllNotesFromType(String type) {
    List<Note> notes = new ArrayList<>();
    String selectQuery = "select * from notes where type like ?";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, new String[]{type});
    ptz_point_count = cursor.getCount();
    Log.d("DatabaseHelper", "cursor getAllNotesFromType count ="+ptz_point_count);     
    if (cursor.moveToFirst()) {
        do {
            Note note = new Note();
            note.setId(cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)));
            note.setType(cursor.getString(cursor.getColumnIndex(Note.COLUMN_TYPE)));
            note.setNote(cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)));
            note.setTimestamp(cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
            notes.add(note);
        } while (cursor.moveToNext());
    }        
    db.close();
    cursor.close();       
    return notes;
}

3.程序调用sql

增删改查

    public void SqlNormalUse(String order,String note,String type) {
        switch (order){
            case "add":             
                db.deleteType(type); //先删除
                db.insertNote(note,type); //再写入
                break;
            case "delete":               
                db.deleteType(type);
                break;
            case "change":
                List<Note> notesType = db.getAllNotesFromType("%"+type+"%");
                Log.i(TAG, "change....... ="+db.ptz_point_count);
                if(db.ptz_point_count==0){
                    Log.i(TAG, "change....不存在");
                    db.insertNote(note,type); //再写入
                }else{  //先删再加
                    Log.i(TAG, "change....存在");
                    db.updateType(note,type); //
                }
                break;
            case "check":
                Log.i(TAG, "check....... ");
                Log.i(TAG, "数据条目数 +"+db.getNotesCount() );
                Log.i(TAG, "=====SqlNormalUse===整体查询=================QQQ" );
                List<Note> notes = db.getAllNotes();
                for (Note note1 : notes){
                    Log.i(TAG, "测试SQLite id +"+note1.getId() );  //获取到了当前写入的数据
                    Log.i(TAG, "测试SQLite Type +"+note1.getType() );  //获取到了当前写入的数据
                    Log.i(TAG, "测试SQLite Note +"+note1.getNote() );  //获取到了当前写入的数据
                }
                break;
            default:
                break;
        }
    }

查询type中包含特定字符的数据

    public void SqlDrawData(String type,Promise p) {
        WritableMap map = Arguments.createMap();
        String sqlDrawData = "";
        List<Note> notesType = db.getAllNotesFromType("%"+type+"%");
        for (Note note : notesType){
            Log.i(TAG, "测试SQLiteType id +"+note.getId() );  //获取到了当前写入的数据
            Log.i(TAG, "测试SQLiteType Type +"+note.getType() );  //获取到了当前写入的数据
           Log.i(TAG, "测试SQLiteType Note +"+note.getNote() );  //获取到了当前写入的数据
           sqlDrawData += note.getNote() ;
       }      
        Log.i(TAG, "sqlDrawData=== : "+sqlDrawData+"---type===:"+type );
        map.putString("sqlDrawData", sqlDrawData);
        p.resolve(map);
    }

4.与json的搭配使用

通过将数据以json方式存储到数据库的方式,节省条目及快速获取有效信息
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值