首页 > 技术文章 > Android SQLite的查询操作

a7e7 2020-11-23 15:03 原文

环境

Windows10(64bit)
Android Studio 4.1 for Windows
SQLite的*.db文件存放在项目的res\rew\(项目根目录\app\src\main\res\rew)目录下

查询方法

SQL语句查询

public String queryAddressCode2(int a) {
      // 打开数据库
        SQLiteDatabase db = helper.getWritableDatabase();
      // 改成查询的SQL语句
        String sql = "select " + Constants.DATA_02 + "," + Constants.DATA_06 + " from " + Constants.DATABASE_TABLE_NAME + " where " + Constants.DATA_03 + " = " + a + ";";
      // 执行查询语句并获取查到的内容
        Cursor cursor = db.rawQuery(sql, null);
      // 看看查询的结果是否为空
        if (cursor == null) {
            return "查了个寂寞";
        }
      // 声明一个字符串来存东西(我记得有另一个更好的方法)
        String outStr = "";
        while (cursor.moveToNext()) {
            outStr = outStr + cursor.getInt(0) + "   " + cursor.getString(1) + "\n";
        }
        cursor.close();  // 用完关闭cursor
        db.close();      // 用完关闭数据库
        Log.d(TAG, outStr);      // 看看有什么东西被打印出来
        return outStr;            // 返回查询的内容(整理后的)

    }

Android API查询

以后用到再写

参考资料

  1. CSDN 【玩转SQLite系列】(四)通过Android提供的API操作SQLite数据库

推荐阅读