Android Open Source - Cardeto Cardeto Sample Database Data Source






From Project

Back to project page Cardeto.

License

The source code is released under:

Apache License

If you think the Android project Cardeto listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ggt.cardetosample.database;
/*  www. j a  v  a  2  s. c  o m*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;


/**
 * Datasource deal with logs  usage in a sqlite database.
 *
 * @author guiguito
 */
public class CardetoSampleDatabaseDataSource {
    // Database fields
    private SQLiteDatabase database;
    private CardetoSampleDatabaseHelper dbHelper;
    private String[] allColumns = {CardetoSampleDatabaseHelper.COLUMN_ID,
            CardetoSampleDatabaseHelper.COLUMN_LOG};

    public CardetoSampleDatabaseDataSource(Context context) {
        dbHelper = new CardetoSampleDatabaseHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public Log createLog(String log) {
        ContentValues values = new ContentValues();
        values.put(CardetoSampleDatabaseHelper.COLUMN_LOG, log);
        long insertId = database.insert(CardetoSampleDatabaseHelper.TABLE_LOG,
                null, values);
        Cursor cursor = database.query(CardetoSampleDatabaseHelper.TABLE_LOG,
                allColumns, CardetoSampleDatabaseHelper.COLUMN_ID + " = "
                        + insertId, null, null, null, null);
        cursor.moveToFirst();
        Log newLog = cursorToLog(cursor);
        cursor.close();
        return newLog;
    }

    public void deleteLog(Log log) {
        long id = log.getId();
        System.out.println("Log deleted with id: " + id);
        database.delete(CardetoSampleDatabaseHelper.TABLE_LOG,
                CardetoSampleDatabaseHelper.COLUMN_ID + " = " + id, null);
    }

    public void clearLogs() {
        database.delete(CardetoSampleDatabaseHelper.TABLE_LOG, null, null);
    }

    public List<Log> getAllLogs() {
        List<Log> logs = new ArrayList<Log>();

        Cursor cursor = database.query(CardetoSampleDatabaseHelper.TABLE_LOG,
                allColumns, null, null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Log log = cursorToLog(cursor);
            logs.add(log);
            cursor.moveToNext();
        }
        // Make sure to close the cursor
        cursor.close();
        return logs;
    }

    private Log cursorToLog(Cursor cursor) {
        Log log = new Log();
        log.setId(cursor.getLong(0));
        log.setLog(cursor.getString(1));
        return log;
    }

}




Java Source Code List

com.ggt.cardeto.CardetoService.java
com.ggt.cardeto.embeddedwebserver.CardetoWebServerModule.java
com.ggt.cardeto.embeddedwebserver.CardetoWebServer.java
com.ggt.cardeto.embeddedwebserver.appinfomodule.AppInfoModule.java
com.ggt.cardeto.embeddedwebserver.clipboardmodule.ClipboardModule.java
com.ggt.cardeto.embeddedwebserver.logcatmodule.LogcatModule.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.GenericSQLiteDatabaseHelper.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.SQLiteModule.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.CSVRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.HtmlRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.JsonRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.TableOutputRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.XmlRenderer.java
com.ggt.cardeto.embeddedwebserver.staticvarsmodule.StaticVarsModule.java
com.ggt.cardeto.externallib.NanoHTTPD.java
com.ggt.cardeto.utils.CardetoConstants.java
com.ggt.cardeto.utils.NetworkUtilsOld.java
com.ggt.cardeto.utils.NetworkUtils.java
com.ggt.cardetosample.TestActivity.java
com.ggt.cardetosample.database.CardetoSampleDatabaseDataSource.java
com.ggt.cardetosample.database.CardetoSampleDatabaseHelper.java
com.ggt.cardetosample.database.Log.java