Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Pursuer (http://pursuer.me).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Pursuer - initial API and implementation
 ******************************************************************************/

import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Main {
    private static void checkTableColumns(final SQLiteDatabase database, final String tableName,
            final String[] columns, final String[] columnTypes) {
        final List<String> cols = getTableColumns(database, tableName);
        for (int i = 0; i < columns.length; i++) {
            boolean found = false;
            for (final String col : cols) {
                if (col.equals(columns[i])) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                database.execSQL("ALTER TABLE " + tableName + " ADD COLUMN " + columns[i] + " " + columnTypes[i]);
            }
        }
    }

    private static List<String> getTableColumns(final SQLiteDatabase database, final String tableName) {
        List<String> ret = null;
        try {
            final Cursor cur = database.rawQuery("SELECT * FROM " + tableName + " LIMIT 1", null);
            if (cur != null) {
                ret = new ArrayList<String>(Arrays.asList(cur.getColumnNames()));
            }
            cur.close();
        } catch (final Exception exception) {
            exception.printStackTrace();
        }
        return ret;
    }
}