如何在SQLite中获取所有插入的记录

时间:2016-08-03 12:35:56

标签: android android-sqlite

Hello Friends我想获取sqlite中的所有记录并希望在Log.can中打印它。有人告诉我如何在android.below中执行此操作是我的code.i不想在数组列表或textview中显示此记录只想在Logcat.i中打印它不知道getWritableDatabase的作用是什么,getredableDatabase.plz给出了它的一些细节。我是android.thanks中的新功能。

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;
    }                              

2 个答案:

答案 0 :(得分: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;
}

希望这会对你有所帮助。

答案 1 :(得分:0)

您调用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;
    }