Thursday, May 2, 2013

[android-developers] Re: program crashes when i try to create a table for my app

I think there should not be a db.close() in the onCreate method.


Thanks.



On Tuesday, April 23, 2013 8:43:06 AM UTC-5, Manish Jain wrote:
here is the code of my database java file

package com.example.docterapp;

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

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

    // private dbhelper ourdbhelper;

    // private final Context ourContext;

    // private SQLiteDatabase ourdatabase;

    SQLiteDatabase db;
    private String Doctor_ID;
    private String Online_status;
    private String Appointment;
    private String Department;
    private String Password;
    private String Name;
    private String Address1;
    private String Address2;
    private String mobile_no;
    private String Patient_ID;
    private String age;
    private String gender;
    private String Ailment;
    private String Previous_report;
    private String Hospital;
    private String Degree;
    private String Grad_College;
    private String Experience;
    private String Avail_time;
    public static String DBNAME = "DO.db";
    public static String TABLE1 = "patient_pers_rec";
    public static String TABLE2 = "doctor_pers_rec";
    public static String TABLE3 = "Patient_medical_rec";
    public static String TABLE4 = "doctor_professional_details";
    public static String TABLE5 = "doctor_availability";
    public static final int DATABASE_VERSION = 1;

    public DatabaseHandler(Context context) {
        super(context, DBNAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
       
       
        //db.openOrCreateDatabase(DBNAME, , null);
        //db = openOrCreateDatabase(;
        db.execSQL("CREATE TABLE " + TABLE1 + " (" + Patient_ID
                + " INTEGER PRIMARY KEY," + Password + " TEXT," + Name
                + " TEXT," + age + " INTEGER," + gender + " TEXT," + mobile_no
                + " INTEGER);");
        // }
        Log.i("INFO","Tb1 made");
        // public void onCreate(SQLiteDatabase db)
        // {

        db.execSQL("CREATE TABLE " + TABLE2 + " (" + Doctor_ID
                + " INTEGER PRIMARY KEY," + Password + " TEXT," + Name
                + " TEXT," + Address1 + " TEXT," + Address2 + " TEXT,"
                + mobile_no + " INTEGER," + ");");

        db.execSQL("CREATE TABLE " + TABLE3 + " (" + Patient_ID
                + " INTEGER PRIMARY KEY," + Ailment + " TEXT,"
                + Previous_report + " TEXT," + ");");

        db.execSQL("CREATE TABLE " + TABLE4 + " (" + Doctor_ID
                + " INTEGER PRIMARY KEY," + Hospital + " TEXT," + Degree
                + " TEXT," + Grad_College + " TEXT," + Department + " TEXT,"
                + Experience + " INTEGER," + ");");

        db.execSQL("CREATE TABLE " + TABLE5 + " (" + Doctor_ID
                + " INTEGER PRIMARY KEY," + Avail_time + " INTEGER,"
                + Online_status + " TEXT," + Appointment + " TEXT );");
        db.close();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE1);
        db.execSQL("drop table if exists" + TABLE2);
        db.execSQL("drop table if exists" + TABLE3);
        db.execSQL("drop table if exists" + TABLE4);
        db.execSQL("drop table if exists" + TABLE5);
        onCreate(db);
    }

    void add(patient_pers_rec patient_pers_rec) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Patient_ID, patient_pers_rec.getPatient_ID());
        values.put(Password, patient_pers_rec.getPassword());
        values.put(Name, patient_pers_rec.getName());
        values.put(age, patient_pers_rec.getage());
        values.put(gender, patient_pers_rec.getGender());

        // Inserting Row
        db.insert(TABLE1, null, values);
        db.close(); // Closing database connection
    }

    void add(doctor_pers_rec doctor_pers_rec) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Patient_ID, doctor_pers_rec.getDoctor_ID());
        values.put(Password, doctor_pers_rec.getPassword());
        values.put(Name, doctor_pers_rec.getName());
        values.put(Address1, doctor_pers_rec.getAddress1());
        values.put(Address2, doctor_pers_rec.getAddress2());
        values.put(mobile_no, doctor_pers_rec.getMobile_no());

        // Inserting Row
        db.insert(TABLE2, null, values);
        db.close(); // Closing database connection
    }

    void add(Patient_medical_rec Patient_medical_rec) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Patient_ID, Patient_medical_rec.getPatient_id());
        values.put(Ailment, Patient_medical_rec.getAilment());
        values.put(Previous_report, Patient_medical_rec.getPrevious_report());

        // Inserting Row
        db.insert(TABLE3, null, values);
        db.close(); // Closing database connection
    }

    void add(doctor_professional_details doctor_professional_details) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Degree, doctor_professional_details.Degree());
        values.put(Department, doctor_professional_details.Department());
        values.put(Experience, doctor_professional_details.getExperience());
        values.put(Grad_College, doctor_professional_details.getGrad_College());
        values.put(Hospital, doctor_professional_details.getHospital());
        values.put(Doctor_ID, doctor_professional_details.getDoctor_ID());

        // Inserting Row
        db.insert(TABLE4, null, values);
        db.close(); // Closing database connection
    }

    void add(doctor_availability doctor_availability) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Doctor_ID, doctor_availability.getDoctor_ID());
        values.put(Appointment, doctor_availability.getAppointment());
        values.put(Avail_time, doctor_availability.getAvail_time());
        values.put(Online_status, doctor_availability.getOnline_status());

        // Inserting Row
        db.insert(TABLE5, null, values);
        db.close(); // Closing database connection
    }

    doctor_pers_rec getRecord(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
       
       

        Cursor cursor = db.query(TABLE2, new String[] { Doctor_ID, Password,
                Name, Address1, Address2, mobile_no }, Doctor_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

       
        doctor_pers_rec Doctor_pers_rec = new doctor_pers_rec(
                Integer.parseInt(cursor.getString(0)), cursor.getString(1),
                cursor.getString(2), cursor.getString(3), cursor.getString(4),
                Integer.parseInt(cursor.getString(5)));
        // return contact
        return Doctor_pers_rec;
    }

    // Getting All Contacts
    public List<doctor_pers_rec> getAllContacts() {
        List<doctor_pers_rec> Doctor_pers_recList = new ArrayList<doctor_pers_rec>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE2;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                doctor_pers_rec Doctor_pers_rec = new doctor_pers_rec();
                Doctor_pers_rec.setPassword(cursor.getString(1));
                Doctor_pers_rec.setName(cursor.getString(2));
                Doctor_pers_rec.setMobile_no(Integer.parseInt(cursor
                        .getString(5)));
                Doctor_pers_rec.setAddress1(cursor.getString(3));
                Doctor_pers_rec.setAddress2(cursor.getString(4));
                Doctor_pers_rec
                        .Doctor_ID(Integer.parseInt(cursor.getString(0)));
                // Adding contact to list
                Doctor_pers_recList.add(Doctor_pers_rec);
            } while (cursor.moveToNext());
        }

        // return contact list
        return Doctor_pers_recList;
    }

}

--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment