【问题标题】:How to fetch all inserted records in SQLite如何在 SQLite 中获取所有插入的记录
【发布时间】:2016-08-03 12:35:56
【问题描述】:

您好朋友,我想获取 sqlite 中的所有记录并希望在 Log 中打印它。谁能告诉我如何在 android 中执行此操作。下面是我的代码。我不想在数组列表或 textview 中显示此记录只是想在 Logcat 中打印它。我不知道 getWritableDatabase 和 getredableDatabase 的作用是什么。请提供一些详细信息。我是 android 的新手。谢谢。

public class DataBase {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "health";
    private static final String TABLE_NAME = "Device_Data";

    private static final String KEY_ID = "id";
    private static final String KEY_Site_Id = "siteId";
    private static final String KEY_Patient_Id = "patientId";
    private static final String KEY_ReadingType = "readingType";
    private static final String KEY_DeviceMACId = "deviceMACId";
    private static final String KEY_DeviceData = "deviceData ";
    private static final String KEY_DeviceType = "deviceType";

    private final Context context;

    private DataBaseHelper dbHelper;
    public SQLiteDatabase db;
    // private static final String TAG = "DataBase";

    static final String DATABASE_CREATE = "create table " + "TABLE_NAME" + "("
            + KEY_ID + " INTEGER PRIMARY KEY ," + KEY_Site_Id + " TEXT ,"
            + KEY_Patient_Id + " TEXT ," + KEY_ReadingType + " TEXT ,"
            + KEY_DeviceMACId + " TEXT ," + KEY_DeviceData + " TEXT ,"
            + KEY_DeviceType + " TEXT ,")";

    public DataBase(Context _context) {
        context = _context;
        dbHelper = new DataBaseHelper(context, DATABASE_NAME, null,
                DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + "Device_Data");
        onCreate(db);
    }

    void addJson(JSONObject json) throws JSONException {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.getVersion();
        System.out.println("Database Version : " + db.getVersion());
        ContentValues values = new ContentValues();
        try {
            values.put(KEY_Site_Id, json.getString("siteId"));

            values.put(KEY_Patient_Id, json.getString("patientId"));
            System.out.println("KEY_PATIENT_ID : "
                    + json.getString("patientId"));

            values.put(KEY_DeviceMACId, json.getString("deviceMACId"));
            System.out.println("KEY_DeviceMACId : "
                    + json.getString("deviceMACId"));

            values.put(KEY_ReadingType, json.getString("readingType"));
            System.out.println("KEY_ReadingType : "
                    + json.getString("readingType"));

            values.put(KEY_DeviceData, json.getString("deviceData"));
            System.out.println("KEY_DeviceData : "
                    + json.getString("deviceData"));

            values.put(KEY_DeviceType, json.getString("deviceType"));
            System.out.println("KEY_DeviceType : "
                    + json.getString("deviceType"));


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            System.out.println("JsonException........");
            e.printStackTrace();
        }

        long res = db.insert("Device_Data", null, values);
        System.out.println("Response Result : " + res);

        db.close();
    }

    public DataBase open() throws SQLException {
        db = dbHelper.getWritableDatabase();
        return this;
    }

    public SQLiteDatabase getDatabaseInstance() {
        return db;
    }                              

【问题讨论】:

标签: android android-sqlite


【解决方案1】:

您可以从数据库中获取所有数据..

// Getting All Contacts
public List<ReportTable> getAllReportDetail() {
    List<ReportTable> reportList = new ArrayList<ReportTable>();

    String selectQuery = "SELECT  * FROM " + yourTABLE_Name;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {


            ReportTable reportTable = new ReportTable();
            reportTable.set_ReportID(Integer.parseInt(cursor.getString(0)));
            reportTable.set_ReportPaymentMode(cursor.getString(1));
            reportTable.set_ReportRechargeMobileNo(cursor.getString(2));


            // Adding contact to list
            reportList.add(reportTable);
        } while (cursor.moveToNext());
    }

    // return contact list
    return reportList;
}

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    您调用 getWritableDatabase 以确保您的数据库可以读取、写入、更新...bla bla..

    记住每次进行操作(更新、删除、插入):

    First: open
    ..do something.....
    Finally: Close.
    

    您可以通过执行以下操作获取表中的所有条目:

    public ArrayList<HealthData> getAllData() {
            ArrayList<HealthData> res = new ArrayList<HealthData>();
    
            Cursor cursor = database.query(TABLE_NAME, allNoteColumn, null, null, null, null, null);
            for(cursor.moveToLast(); !cursor.isBeforeFirst(); cursor.moveToPrevious()) {
    
    
                HealthData data = cursorToData(cursor);
                res.add(data);
            }
            cursor.close();
            return res;
        }
    
    
        private HealthData cursorToData(Cursor cursor) {
            HealthData data = new HealthData(
                    cursor.getString(0),
                    cursor.getString(1),
                    cursor.getString(2),
                    cursor.getString(3));
                // depending on how many column in your table, above is 4 column.
            return data;
        }
    

    【讨论】:

    • 只是好奇。您的光标向后移动的任何原因?
    • 我正在创建 cursorToData 方法以使其在其他情况下可重用。
    • 那应该没关系吧?该方法只查看一行。我的意思是 for 循环。
    猜你喜欢
    • 2019-03-20
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多