您的位置:首页 > 移动开发 > Android开发

android sqlite简单的sql语句介绍

2014-12-09 22:12 495 查看
android sqlite的sql语句 不能删除一个已经存在的字段,或者更改一个已经存在的字段的名称、数据类型、限定符等等。
sqlite的数据类型:
1.NULL:空值。

2.INTEGER:带符号的整型,具体取决有存入数字的范围大小。

3.REAL:浮点数字,存储为8-byte IEEE浮点数。

4.TEXT:字符串文本。

5.BLOB:二进制对象。

简单的sql语句:
一、创建表

String CREATE_NEWS = "create table weiboTb ("
+ "id integer primary key autoincrement, "
+ "title text, "
+ "content text, "
+ "publishdate integer,"
+ "commentcount integer)";
db.execSQL(CREATE_NEWS);

二、插入数据

public long insert(String table, String nullColumnHack, ContentValues values)

可以看到,insert方法接收三个参数,第一个参数是表名,第二个参数通常都用不到,直接传null,第三个参数则是一个封装了待存储数据的
ContentValues对象。
代码:

SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", "这是一条微博的标题");
values.put("content", "这是一条微博的内容");
values.put("publishdate", System.currentTimeMillis());
long id = db.insert("weiboTb", null, values);

三、删除数据
[java] view
plaincopy





public int delete(String table, String whereClause, String[] whereArgs)

delete()方法接收三个参数,第一个参数同样是表名,第二和第三个参数用于指定删除哪些行,对应了SQL语句中的where部分。

其作用相当于如下SQL语句:

[sql] view plaincopy





delete from weiboTb where commentcount=0;

代码:

SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("weiboTb", "commentcount = ?", new String[] {"0"});

四、修改数据

[java] view
plaincopy





public int update(String table, ContentValues values, String whereClause, String[] whereArgs)

其作用相当于如下SQL语句:

[sql] view plaincopy





update weiboTb set title='heihiehiehieh' where id=2;

代码:

SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", "xxxxxxx");
db.update("weiboTb", values, "id = ?", new String[] {"2"});

五、查询数据
Android当中也提供了直接使用原生SQL语句来查询数据库表的方法,即SQLiteDatabase中的rawQuery()方法,方法定义如下:

[java] view plaincopy

public Cursor rawQuery(String sql, String[] selectionArgs)

其中,rawQuery()方法接收两个参数,第一个参数接收的就是一个SQL字符串,第二个参数是用于替换SQL语句中占位符(?)的字符串数组。 rawQuery()方法返回一个Cursor对象,所有查询到的数据都是封闭在这个对象当中的,我们只要一一取出就可以了。

Android专门提供了一种封装好的API,使得我们不用编写SQL语句也能查询出数据,即SQLiteDatabase中的query()方法。query()提供了三个方法重
载,其中参数最少的一个也有七个参数,我们来看下方法定义:

[java] view plaincopy





public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having, String orderBy)

其中第一参数是表名,表示我们希望从哪张表中查询数据。第二个参数用于指定去查询哪几列,如果不指定则默认查询所有列。第三、第四个参数用于去约束查询某一行或某几行的数据,不指定则默认是查询所有行的数据。第五个参数用于指定需要去group by的列,不指定则表示不对查询结果进行group by操作。第六个参数用于对group
by之后的数据进行进一步的过滤,不指定则表示不进行过滤。第七个参数用于指定查询结果的排序方式,不指定则表示使用默认的排序方式。

代码:

[java] view plaincopy





SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("weiboTb", null, null, null, null, null, null);

可以看到,将第一个表名参数指定成news,然后后面的六个参数我们都用不到,就全部指定成null。

那如果是我们想查询news表中所有评论数大于零的新闻该怎么写呢?代码如下所示:

[java] view plaincopy





SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("weiboTb", null, "commentcount>?", new String[]{"0"}, null, null, null);

由于第三和第四个参数是用于指定约束条件的,所以我们在第三个参数中指明了commentcount>?,然后在第四个参数中通过一个String数组来替换占位符,这样查到的结果就是news表中所有评论数大于零的新闻了。那么其它的几个参数呢?仍然用不到,所以还是只能传null。

通过查询后,获取到一个cursor,再从cursor中取出值

然后我们可以看到,query()方法的返回值是一个Cursor对象,所有查询到的数据都是封装在这个对象中的,所以我们还需要将数据逐一从Cursor对象中取出,然后设置到News实体类当中,如下所示:

[java] view plaincopy





List<Weibo> WeiboList = new ArrayList<Weibo>();
if (cursor != null && cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String content = cursor.getString(cursor.getColumnIndex("content"));
Date publishDate = new Date(cursor.getLong(cursor.getColumnIndex("publishdate")));
int commentCount = cursor.getInt(cursor.getColumnIndex("commentcount"));
Weibo weibo = new Weibo();
weibo.setId(id);
weibo.setTitle(title);
weibo.setContent(content);
weibo.setPublishDate(publishDate);
weibo.setCommentCount(commentCount);
weiboList.add(weibo);
} while (cursor.moveToNext());
}

分页查询

select title,content from users where commentcount > 0 order by publishdate desc limit 10
offset 10;
其中limit表示本次查询的数量 offset表示偏移量 表示从第几条数据开始查询

聚合函数用法:
1、查询表中一共有多少行
执行select语句使用的还是SQLiteDatabase中的rawQuery()方法。下面我们来尝试一下,比如说想要统计weiboTb表中一共有多少行,就可以这样写:

[java] view plaincopy





SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor c = db.rawQuery("select count(1) from weiboTb", null);
if (c != null && c.moveToFirst()) {
int count = c.getInt(0);

}
c.close();

可以看到,在rawQuery()方法中我们指定了一个聚合查询语句,其中count(1)就是用于去统计一共有多少行的。当然这里并不一定要用count(1),使用
count(*)或者count(主键)都可以。然后rawQuery()方法返回的是一个Cursor对象,我们从这个Cursor当中取出第一行第一列的数据,这也就是统计出的
结果了。

2、那如果我们想要统计出weiboTb表中评论的总数量该怎么写呢?代码如下所示:

[java] view plaincopy





SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor c = db.rawQuery("select sum(commentcount) from weiboTb", null);
if (c != null && c.moveToFirst()) {
int count = c.getInt(0);

}
c.close();

我们发现,代码基本是非常相似的,只不过查询语句当中count()函数替换成了sum()函数。当然了,sum()函数要求传入一个指定的列名,表示我们要
汇总这一列的总合,因此这里我们传入了commentcount这一列。
可以总结出一些结论,聚合函数都是要使用rawQuery()方法进行SQL查询

六、修改表的操作

添加字段
增加一列
- ALTER TABLE 表名 ADD COLUMN 列名 数据类型 限定符

db.execSQL("alter table comment add column publishdate integer");

改变表名
改变表名
- ALTER TABLE 旧表名 RENAME TO 新表名
db.execSQL("alter table students
rename to teachers");

七、删除表
删除表 - DROP TABLE 表名
DROP TABLE Teachers;
或者DROP TABLE IF EXISTS Teachers;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: