当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程开发中SQLite使用介绍

Android编程开发中SQLite使用介绍

2018年11月01日  | 移动技术网IT编程  | 我要评论

赵奕菲,岁乐纪,穿比基尼的外星人下载

android开发中sqlite使用介绍

插入数据

方法public long insert(string table, string nullcolumnhack, contentvalues values);

参数string table:表名 参数string nullcolumnhack:当values为null或是空的时候,表中值设为null的字段。 参数contentvalues values:要插入的值。

例:

private void insert(sqlitedatabase db){
//实例化常量值
contentvalues cvalue = new contentvalues();
cvalue.put("product_id","001");
cvalue.put("product_name","t恤衫");
cvalue.put("product_type","衣服");
cvalue.put("sale_price",1000);
cvalue.put("purcgase_price",500);
cvalue.put("regist_date","2009-09-20");
//调用insert()方法插入数据
db.insert("product",null,cvalue);
}

分析:

public long insert(string table, string nullcolumnhack, contentvalues values) {
        try {
            return insertwithonconflict(table, nullcolumnhack, values, conflict_none);
        } catch (sqlexception e) {
            log.e(tag, "error inserting " + values, e);
            return -1;
        }
    }

public long insertwithonconflict(string table, string nullcolumnhack,
            contentvalues initialvalues, int conflictalgorithm) {
        acquirereference();
        try {
            stringbuilder sql = new stringbuilder();
            sql.append("insert");
            sql.append(conflict_values[conflictalgorithm]);
            sql.append(" into ");
            sql.append(table);
            sql.append('(');

            object[] bindargs = null;
            int size = (initialvalues != null && !initialvalues.isempty())
                    ? initialvalues.size() : 0;
            if (size > 0) {
                bindargs = new object[size];
                int i = 0;
                for (string colname : initialvalues.keyset()) {
                    sql.append((i > 0) ? "," : "");
                    sql.append(colname);
                    bindargs[i++] = initialvalues.get(colname);
                }
                sql.append(')');
                sql.append(" values (");
                for (i = 0; i < size; i++) {
                    sql.append((i > 0) ? ",?" : "?");
                }
            } else { // 当initialvalues为null或为空时 
                sql.append(nullcolumnhack + ") values (null"); 
            }
            sql.append(')');

            sqlitestatement statement = new sqlitestatement(this, sql.tostring(), bindargs);
            try {
                return statement.executeinsert();
            } finally {
                statement.close();
            }
        } finally {
            releasereference();
        }
    }

从上面的源码中分析知道,最后执行的还是sql语句。所以是根据参数拼接成sql语句。
注:从上面的源码知道,在使用insert()方法时,需要注意一点。就是参数string nullcolumnhack和contentvalues values不能同时为null,且nullcolumnhack一定要是表中的not null 约束的字段名。在同时为null时,拼接出来的sql语句不是正常的sql语句。

删除数据

方法public int delete(string table, string whereclause, string[] whereargs);

参数string table:表名 参数string whereclause:删除的条件的属性名 参数string[] whereargs:删除的条件的属性的值数组,这个数组中的值会安顺序代替whereclause中的”?”符号。

例:

private void delete(sqlitedatabase db) {
//删除条件
string whereclause = "product_id=?";
string[] whereargs = {"002"};
//执行删除
db.delete("product",whereclause,whereargs);
}

源码分析:

public int delete(string table, string whereclause, string[] whereargs) {
        acquirereference();
        try {
            sqlitestatement statement =  new sqlitestatement(this, "delete from " + table +
                    (!textutils.isempty(whereclause) ? " where " + whereclause : ""), whereargs);
            try {
                return statement.executeupdatedelete();
            } finally {
                statement.close();
            }
        } finally {
            releasereference();
        }
    }

从源码知道,删除语句最终也是拼接成sql语句。当whereclause不为null 或”“ 时,sql语句就会加上where whereclause 子句。最后string[] whereargs数组中的值会代替whereclause中的”?“符号拼接成最终的sql语句。

更新数据

方法public int update(string table, contentvalues values, string whereclause, string[] whereargs);

参数string table:表名 参数contentvalues values:更新的值放在这个values里面 参数string whereclause:删除的条件的属性名 参数string[] whereargs:删除的条件的属性的值数组,这个数组中的值会安顺序代替whereclause中的”?”符号。

例:

private void update(sqlitedatabase db) {  
//实例化内容值 contentvalues values = new contentvalues();  
//在values中添加内容  
values.put("product_type","工具");  
//修改条件  
string whereclause = "product_id=?";  
//修改添加参数  
string[] whereargs={string.valuesof("007")};  
//修改  
db.update("product",values,whereclause,whereargs);  
}  

源码:

public int update(string table, contentvalues values, string whereclause, string[] whereargs) {
        return updatewithonconflict(table, values, whereclause, whereargs, conflict_none);
    }

public int updatewithonconflict(string table, contentvalues values,
            string whereclause, string[] whereargs, int conflictalgorithm) {
        if (values == null || values.isempty()) {
            throw new illegalargumentexception("empty values");
        }

        acquirereference();
        try {
            stringbuilder sql = new stringbuilder(120);
            sql.append("update ");
            sql.append(conflict_values[conflictalgorithm]);
            sql.append(table);
            sql.append(" set ");

            // move all bind args to one array
            int setvaluessize = values.size();
            int bindargssize = (whereargs == null) ? setvaluessize : (setvaluessize + whereargs.length);
            object[] bindargs = new object[bindargssize];
            int i = 0;
            for (string colname : values.keyset()) {
                sql.append((i > 0) ? "," : "");
                sql.append(colname);
                bindargs[i++] = values.get(colname);
                sql.append("=?");
            }
            if (whereargs != null) {
                for (i = setvaluessize; i < bindargssize; i++) {
                    bindargs[i] = whereargs[i - setvaluessize];
                }
            }
            if (!textutils.isempty(whereclause)) {
                sql.append(" where ");
                sql.append(whereclause);
            }

            sqlitestatement statement = new sqlitestatement(this, sql.tostring(), bindargs);
            try {
                return statement.executeupdatedelete();
            } finally {
                statement.close();
            }
        } finally {
            releasereference();
        }
    }

从源码中看出values不能为null或空,否则会抛异常。

查询数据

方法public cursor query(string table, string[] columns, string selection,
string[] selectionargs, string groupby, string having,
string orderby) ;

参数string table:表名 参数string[] columns:要选取的字段 参数string selection:选取条件的字段名 参数string[] selectionargs:选择条件的值,这个数据里面的值会逐一替代selection中的”?”。 参数groupby:把表分组的字段名 参数string having:对表分组后的过滤条件 参数string orderby:排序规则asc ,desc 。

例:

cursor cursor = db.query(tablename , new string[]{"product_id" , "product_type" , "product_name" , "sale_price" , "purcgase_price" , "regist_date"  } , "sale_price >=?" , new string[]{"100"} ,"product_type"  , null , "product_id asc");

cursor类介绍

getcount()获得总的数据项数 isfirst()判断是否第一条记录 islast()判断是否最后一条记录 movetofirst()移动到第一条记录 movetolast()移动到最后一条记录 move(int offset)移动到指定记录 movetonext()移动到下一条记录 movetoprevious()移动到上一条记录 getcolumnindexorthrow(string columnname)根据列名称获得列索引 getint(int columnindex)获得指定列索引的int类型值 getstring(int columnindex)获得指定列缩影的string类型值。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网