vendredi 28 août 2015

data not going into database and not coming from database in android

This is written by me. There is something wrong with this code. When I debugged the code it is terminating in this file.So help is something which I need badly. data is not going into the database, I'm not able to retrieve data from database. Don't know whether the database, table create or not.

package com.Prasad.formdetails;

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

public class DatabaseHandler extends SQLiteOpenHelper {
  //database version
  private static final int Database_Version =1;
  //database name
  private static final String Database_Name = "persondetails";
  //table name
  private static final String Table_Name="persondetails";
  //table column names
  private static final String key_personName="Name";
  private static final String key_mailId="mailID";
  private static final String key_phone="Phone";
  @Override
  public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String CREATE_DETAILS_TABLLE="CREATE TABLE" + 
    Table_Name + "(" + key_personName + "text" + key_mailId + "text primary key" + key_phone + "text" + ")";
    db.execSQL(CREATE_DETAILS_TABLLE);
  }
  //constructor as no default constructor in super
    public DatabaseHandler(Context context){
        super(context,Database_Name, null, Database_Version);
    }


    @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    //delete the existing table while updating
    db.execSQL("drop table if exists" + Table_Name);
    //again create table
    onCreate(db);
  }

  void addPerson(Person person) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(key_personName, person.getName()); // Person Name
    values.put(key_mailId, person.getId()); // Person mail id
    values.put(key_phone, person.getPhone()); // Person Phone

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

    Person getPerson(String id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor=db.query(Table_Name,new String[] {key_personName,key_mailId,key_phone}, key_mailId + "=?",
            new String[]{id},null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Person person = new Person(cursor.getString(0),
            cursor.getString(1), cursor.getString(2));
    // return contact
    return person;
}
}

Aucun commentaire:

Enregistrer un commentaire