Android 原生数据库sqlite 基础回顾

建表 —SQLiteOpenHelper
升级表方式
表关联
传统的存储数据方式
传统的修改和删除数据方式
传统的查询数据方式
传统的聚合函数用法

参考链接


建表 —SQLiteOpenHelper

这个类集创建和升级数据库于一身,并且自动管理了数据库版本
第一次创建表会回调onCreate,后面就不会了

// 获取到SQLiteDatabase的实例,数据库表就会自动创建了
SQLiteOpenHelper dbHelper = new MySQLiteHelper(this, "demo.db", null, 1);
SQLiteDatabase db = dbHelper.getWritableDatabase();

public class MySQLiteHelper extends SQLiteOpenHelper {
	
 public static final String CREATE_NEWS = "create table news ("
   + "id integer primary key autoincrement, "
   + "title text, "
   + "content text, "
   + "publishdate integer,"
   + "commentcount integer)";
 
 public MySQLiteHelper(Context context, String name, CursorFactory factory,
   int version) {
  super(context, name, factory, version);
 }
 
 @Override
 public void onCreate(SQLiteDatabase db) {
  db.execSQL(CREATE_NEWS);
 }
    ...
}

参考
https://blog.youkuaiyun.com/guolin_blog/article/details/38556989

升级表方式

当原先的数据库需要增加新的表新的表或更新新的东西时(反正就是有变化),那么需要升级。
对于SQLiteOpenHelper 当版本号变化的时候会回调onUpgrade方法。
这里主要要对onUpgrade进行处理(要兼容旧表数据迁移)

SQLiteOpenHelper dbHelper = new MySQLiteHelper(this, "demo.db", null, 2);
SQLiteDatabase db = dbHelper.getWritableDatabase();

public class MySQLiteHelper extends SQLiteOpenHelper {
 
 public static final String CREATE_NEWS = "create table news ("
   + "id integer primary key autoincrement, "
   + "title text, "
   + "content text, "
   + "publishdate integer,"
   + "commentcount integer)";
	
 public static final String CREATE_COMMENT = "create table comment ("
   + "id integer primary key autoincrement, "
   + "content text)";
 
 public MySQLiteHelper(Context context, String name, CursorFactory factory,
   int version) {
  super(context, name, factory, version);
 }

 
 @Override
 public void onCreate(SQLiteDatabase db) {
  db.execSQL(CREATE_NEWS);
  db.execSQL(CREATE_COMMENT);
 }
 
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  switch (oldVersion) {
  case 1:
   db.execSQL(CREATE_COMMENT);
  default:
  }
 }
 
}

https://blog.youkuaiyun.com/guolin_blog/article/details/39151617

表关联

1 一对一关联的实现方式是用外键,多对一关联的实现方式也是用外键,多对多关联的实现方式是用中间表
2 一对一的时候外键加在哪一张表上都可以,但多对一的时候关键必须要加在多方的表中

eg: 新闻,摘要,评论

  • 一对一
    相互引用,如果想让两张表之间建立一对一的关系,一般就只能通过外键的方式来实现了
  • 多对一
    一条新闻可以有很多条评论,但是一条评论只能是属于一条新闻的
  • 多对多
    我们都知道新闻网站是会将新闻进行种类划分的,这样用户就可以选择自己喜欢的那一类新闻进行浏览,比如说网易新闻中就会有头条、科技、娱乐、手机等等种类。每个种类下面当然都会有许多条新闻,而一条新闻也可能是属于多个种类的,比如iPhone6发布的新闻既可以属于手机种类,也可以属于科技种类,甚至还可以上头条。因此,新闻和种类之间就是一种多对多的关系

https://blog.youkuaiyun.com/guolin_blog/article/details/39207945

传统的存储数据方式

public long insert(String table, String nullColumnHack, ContentValues values)
缺点:
手动对关联表的外键进行存储
没有提供批量存储的功能

SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", "这是一条新闻标题");
values.put("content", "这是一条新闻内容");
values.put("publishdate", System.currentTimeMillis());
long id = db.insert("news", null, values);
传统的修改和删除数据方式
  • UPDATE

public int update(String table, ContentValues values, String whereClause, String[] whereArgs)
等价于 update news set title=‘今日iPhone6发布’ where id=2;

SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", "今日iPhone6发布");
db.update("news", values, "id = ?", new String[] {"2"});
  • DELETE

public int delete(String table, String whereClause, String[] whereArgs)
等价于
delete from news where commentcount=0;

SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("news", "commentcount = ?", new String[] {"0"});
传统的查询数据方式
public Cursor rawQuery(String sql, String[] selectionArgs)

public Cursor query(String table, String[] columns, String selection,
            String[] selectionArgs, String groupBy, String having,
            String orderBy)
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("news", null, "commentcount>?", new String[]{"0"}, null, null, null);


List<News> newsList = new ArrayList<News>();
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"));
  News news = new News();
  news.setId(id);
  news.setTitle(title);
  news.setContent(content);
  news.setPublishDate(publishDate);
  news.setCommentCount(commentCount);
  newsList.add(news);
 } while (cursor.moveToNext());
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值