Android SqliteDatabase Command insertIntoTable(SQLiteDatabase database, String table, String[] data)

Here you can find the source of insertIntoTable(SQLiteDatabase database, String table, String[] data)

Description

insert Into Table

Declaration

public static boolean insertIntoTable(SQLiteDatabase database,
            String table, String[] data) throws SQLiteConstraintException 

Method Source Code

//package com.java2s;

import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;

public class Main {
    public static boolean insertIntoTable(SQLiteDatabase database,
            String table, String[] data) throws SQLiteConstraintException {
        String values = "";
        for (int i = 0; i < data.length; i++) {
            if (data[i].trim().length() != 0) {
                if (i == data.length - 1)
                    values += quote(data[i]);
                else
                    values += quote(data[i]) + ", ";
            } else {
                if (i == data.length - 1)
                    values += "null";
                else
                    values += "null ,";
            }/*from w w  w.  j a va2s .c  o  m*/
        }
        String query = String.format("insert into %s values (%s);", table,
                values);
        database.execSQL(query);
        return true;
    }

    public static String quote(String s) {
        return "\'" + s + "\'";
    }
}

Related

  1. executeSqlStatements(SQLiteDatabase db, String[] statements)
  2. executeSqlStatementsInTx(SQLiteDatabase db, String[] statements)
  3. getListOfDays(SQLiteDatabase db)
  4. selectFromTable(SQLiteDatabase db, String table, String sample)
  5. updateTable(SQLiteDatabase database, String table, String id, String[] newValues)
  6. getTaskCursor(SQLiteDatabase database, String name)
  7. SQLiteSanitize(String input)