0

I created an application for rails. Initially it is running well. Now i am trying to add an extra column to the database. In the database the additional column is not getting updated however I try. I used the onUpgrade() method and altered table but it didnt work. I tried to change the column in sqlite database browser it was updated there but i dont know how to replicate it back into the front end code I tried all the possibilities to that . Now that I created a new project and tried to insert that column again. I couldnt find out the package name in "data/data". Please could anybody tell me why i coulnt find the database and why am i unable to add new column to the database. Please be polite and patient coz I am a beginner. Thanks in advance

2
  • completely uninstall the app from the device and clean the project. Try now. Commented Mar 22, 2014 at 9:02
  • whenever u update a column in SQLite table changes reflects only after reinstalling the app. Commented Mar 22, 2014 at 9:32

2 Answers 2

2

Actually,when you use the onUpgrade() method you haven't change the database version code.

when you create SQLiteOpenHandler class object then you have to pass a database version code in it. when you pass a new version code in the constructor of SQLiteOpenHandler class then onUpgrade() method is called.

i.e.

SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

previously if you pass version = 1 then you have to pass a new version code in it i.e 2 or 3 or some new integer value then your onUpgrade() method is called and it will solve your problem.

For more details you can go throught this link

and Another issue you have raised that you haven't found package name in data/data folder please check that the package name of your new project will be the same as your previous one.

Sign up to request clarification or add additional context in comments.

Comments

-1
    Refer this below link

    http://www.vogella.com/tutorials/AndroidSQLite/article.html
    -----------------------------------------------------------

http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/

Essential Block of the code in this part..




package de.vogella.android.sqlite.first;

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

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

public class CommentsDataSource {

  // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
      MySQLiteHelper.COLUMN_COMMENT };

  public CommentsDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public Comment createComment(String comment) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_COMMENT, comment);
    long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
        values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
        allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
        null, null, null);
    cursor.moveToFirst();
    Comment newComment = cursorToComment(cursor);
    cursor.close();
    return newComment;
  }

  public void deleteComment(Comment comment) {
    long id = comment.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
        + " = " + id, null);
  }

  public List<Comment> getAllComments() {
    List<Comment> comments = new ArrayList<Comment>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Comment comment = cursorToComment(cursor);
      comments.add(comment);
      cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return comments;
  }

  private Comment cursorToComment(Cursor cursor) {
    Comment comment = new Comment();
    comment.setId(cursor.getLong(0));
    comment.setComment(cursor.getString(1));
    return comment;
  }
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.