CreateDatabase.java :  » Web » plantcareproject » com » watering » plan » Android Open Source

Android Open Source » Web » plantcareproject 
plantcareproject » com » watering » plan » CreateDatabase.java
package com.watering.plan;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class CreateDatabase extends Activity {

  SQLiteDatabase myDB = null;

  public void InsertDataSmall(String PlantID, String Season,
      String Temperature, String Humidity, String Light, String Soil,
      String Watering, String Fertilizer) {
    String TableName = "PlantInfoSmall";
    myDB.execSQL("INSERT INTO " + TableName
        + " (PlantID, Season, Temperature, Humidity, Light,"
        + "Soil,`` Watering, Fertilizer)" + " VALUES (" + PlantID + ","
        + Season + "," + Temperature + "," + Humidity + "," + Light
        + "," + Soil + "," + Watering + "," + Fertilizer + ");");
  }

  public void InsertDataLarge(String PlantID, String PlantType,
      String Description, String Origin, String Environment,
      String Foliage, String Flowering, String Season, String Fragrant,
      String GrowingEase, String Temperature, String Humidity,
      String Light, String Soil, String Watering, String Fertilizer,
      String Pests, String Propagation, String Tips) {
    String TableName = "PlantInfoLarge";
    myDB.execSQL("INSERT INTO " + TableName
        + "(PlantID, PlantType, Description, Origin,"
        + "Environment, Foliage, Flowering, Season,"
        + "Fragrant, GrowingEase, Temperature, Humidity,"
        + "Light, Soil, Watering, Fertilizer, Pests,"
        + "Propagation, Tips)" + " VALUES (" + PlantID + ","
        + PlantType + "," + Description + "," + Origin + ","
        + Environment + "," + Foliage + "," + Flowering + "," + Season
        + "," + Fragrant + "," + GrowingEase + "," + Temperature + ","
        + Humidity + "," + Light + "," + Soil + "," + Watering + ","
        + Fertilizer + "," + Pests + "," + Propagation + "," + Tips
        + "); ");
  }

  public void InsertDataReg(String PlantID, String Season,
      String Temperature, String Humidity, String Light, String Soil,
      String Watering, String Fertilizer) {
    String TableName = "RegisteredPlants";
    myDB.execSQL("INSERT INTO " + TableName
        + " (PlantID, Season, Temperature, Humidity, Light,"
        + "Soil, Watering, Fertilizer)" + " VALUES (" + PlantID + ","
        + Season + "," + Temperature + "," + Humidity + "," + Light
        + "," + Soil + "," + Watering + "," + Fertilizer + ");");

  }

  public void retreive() {
    String Data = "";
    try {
      /* retrieve data from database */
      Cursor c = myDB.rawQuery("SELECT * FROM " + "PlantSmall", null);

      int Column1 = c.getColumnIndex("Field1");
      int Column2 = c.getColumnIndex("Field2");

      // Check if our result was valid.
      c.moveToFirst();
      if (c != null) {
        // Loop through all Results
        do {
          String Name = c.getString(Column1);
          int Age = c.getInt(Column2);
          Data = Data + Name + "/" + Age + "\n";
        } while (c.moveToNext());
      }
      TextView tv = new TextView(this);
      tv.setText(Data);
      setContentView(tv);
    } catch (Exception e) {
      Log.e("Error", "Error", e);
    } finally {
      if (myDB != null)
        myDB.close();
    }

  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String PlantSmall = "PlantInfoSmall";
    String PlantLarge = "PlantInfoLarge";
    String PlantReg = "RegisteredPlants";

    String Data = "";

    /* Create a Database. */
    try {
      myDB = this.openOrCreateDatabase("WateringPlanDatabase",
          MODE_PRIVATE, null);

      /* Create a Table in the Database. */
      myDB.execSQL("CREATE TABLE IF NOT EXISTS "
          + PlantLarge
          + " (PlantID INT, PlantType INT, Description VARCHAR, Origin VARCHAR,"
          + "Environment VARCHAR, Foliage INT, Flowering INT, Season VARCHAR,"
          + "Fragrant INT, GrowingEase INT, Temperature VARCHAR, Humidity INT,"
          + "Light INT, Soil VARCHAR, Watering INT, Fertilizer VARCHAR, Pests VARCHAR,"
          + "Propagation VARCHAR, Tips VARCHAR);");

      myDB.execSQL("CREATE TABLE IF NOT EXISTS "
          + PlantSmall
          + " (PlantID INT, Season VARCHAR, Temperature VARCHAR, Humidity INT, Light INT,"
          + "Soil VARCHAR, Watering INT, Fertilizer VARCHAR);");

      myDB.execSQL("CREATE TABLE IF NOT EXISTS "
          + PlantReg
          + " (PlantRID INT, PlantName VARCHAR, PlantID INT, PlantCat INT, PotSize INT, Foliage INT,"
          + "Light INT, Water INT, LastDate Date);");

      /* retrieve data from database */
      Cursor c = myDB.rawQuery("SELECT * FROM " + PlantSmall, null);

      int Column1 = c.getColumnIndex("Field1");
      int Column2 = c.getColumnIndex("Field2");

      // Check if our result was valid.
      c.moveToFirst();
      if (c != null) {
        // Loop through all Results
        do {
          String Name = c.getString(Column1);
          int Age = c.getInt(Column2);
          Data = Data + Name + "/" + Age + "\n";
        } while (c.moveToNext());
      }
      TextView tv = new TextView(this);
      tv.setText(Data);
      setContentView(tv);
    } catch (Exception e) {
      Log.e("Error", "Error", e);
    } finally {
      if (myDB != null)
        myDB.close();
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.