Android Open Source - android-tools Database Table






From Project

Back to project page android-tools.

License

The source code is released under:

MIT License

If you think the Android project android-tools 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 net.comfreeze.lib.db;
//from   ww  w  .j a  v  a  2s .c o m
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

abstract public class DatabaseTable {
    private static final String TAG = DatabaseTable.class.getSimpleName();

    protected SQLiteDatabase db;
    public Context currentContext;

    public DatabaseTable(Context context, SQLiteDatabase database) {
        Log.d(TAG, "DatabaseTable loading...");
        currentContext = context;
        db = database;
    }

    public void close() {
        if (db.isOpen()) {
            db.close();
        }
    }

    public int count(String table) {
        String sql = String.format("SELECT COUNT(1) AS id_count FROM %s", table);
        Cursor cursor = db.rawQuery(sql, null);
        int count = 0;
        if (cursor.moveToFirst()) {
            count = cursor.getInt(0);
        }
        log("number of records: " + count);
        cursor.close();
        return count;
    }

    public int getNextId(String table, String column) {
        if (column == null) {
            column = "id";
        }
        Log.d(TAG, "DatabaseTable: getting next unique ID for " + table);
        String query = "SELECT MAX(" + column + ") AS max_id FROM " + table;
        Cursor cursor = db.rawQuery(query, null);
        int id = 1;
        if (cursor.moveToFirst()) {
            Log.d(TAG, "Resulting columns: " + cursor.getColumnCount());
            id = cursor.getInt(0) + 1;
        }
        if (id < 1) {
            id = 1;
        }
        Log.d(TAG, "DatabaseTable: resulting unique ID: " + id);
        return id;
    }

    public Cursor rawQuery(String query) {
        return db.rawQuery(query, null);
    }

    public Cursor rawQuery(String query, String[] selectionArgs) {
        return db.rawQuery(query, selectionArgs);
    }

    protected void log(String message) {
        Log.d(TAG, "[DB] " + message);
    }
}




Java Source Code List

net.comfreeze.lib.BundleBuilder.java
net.comfreeze.lib.CFZApplication.java
net.comfreeze.lib.ContentValueBuilder.java
net.comfreeze.lib.FragmentMap.java
net.comfreeze.lib.adapter.HeaderListAdapter.java
net.comfreeze.lib.adapter.IHeaderListAdapter.java
net.comfreeze.lib.adapter.SeparatedListAdapter.java
net.comfreeze.lib.api.BaseAPI.java
net.comfreeze.lib.api.RestAPI.java
net.comfreeze.lib.api.XMLAPI.java
net.comfreeze.lib.api.helper.CursorHelper.java
net.comfreeze.lib.api.helper.JSONHelper.java
net.comfreeze.lib.api.helper.ModelHelper.java
net.comfreeze.lib.api.xml.WordpressAPI.java
net.comfreeze.lib.audio.SoundManager.java
net.comfreeze.lib.db.DatabaseHelper.java
net.comfreeze.lib.db.DatabaseTable.java
net.comfreeze.lib.db.helper.HelperCursor.java
net.comfreeze.lib.db.model.CFZModel.java
net.comfreeze.lib.db.model.FieldColumnMap.java
net.comfreeze.lib.fragments.CFZListFragment.java
net.comfreeze.lib.provider.CFZSimpleProvider.java
net.comfreeze.lib.service.CFZService.java
net.comfreeze.lib.ui.SupportFragmentActivity.java
net.comfreeze.lib.ui.dialog.CFZDialogProgress.java
net.comfreeze.lib.ui.fragment.CFZFragmentBase.java
net.comfreeze.lib.views.BiScrollView.java
net.comfreeze.lib.views.CFZViewHelper.java
net.comfreeze.lib.views.FlowLayout.java
net.comfreeze.lib.views.GestureHelper.java
net.comfreeze.lib.views.HeaderListView.java
net.comfreeze.lib.views.ResizingView.java
net.comfreeze.lib.views.ViewCollection.java
net.comfreeze.lib.views.ViewUtils.java
net.comfreeze.lib.xml.XMLParser.java
net.comfreeze.lib.xml.wordpress.FeedXmlParser.java