0

This is a very basic question. I am just trying to understand how SQLite database works. So, here's what I do: In the code section below which is taken from notepad tutorial 3rd exercise, I change KEY_TITLE to KEY_NAME and all place I find title to name. And the application crashes. Why does this happen?

public static final String KEY_TITLE = "title";
//change to:     public static final String KEY_NAME = "name";
public static final String KEY_BODY = "body";
public static final String KEY_ROWID = "_id";

private static final String TAG = "NotesDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

/**
 * Database creation sql statement
 */
private static final String DATABASE_CREATE =
    "create table notes (_id integer primary key autoincrement, "
    + "title text not null, body text not null);";

/*change to: private static final String DATABASE_CREATE = "create table notes (_id integer primary key autoincrement, " + "name text not null, body text not null);"; */

1
  • 1
    Are you getting any errors in the logs? Any errors from the program before it crashes? Commented Jul 14, 2011 at 23:08

2 Answers 2

1

It would be helpful if you copied the stack trace (using logcat / DDMS) or copied your entire SQLiteDBAdapter, but just looking at what you posted, you definitely have a problem in that you're using the wrong field name in the sqlite database create statement.

The "title" field should be renamed "name" to match your changed column name.

Change:

private static final String DATABASE_CREATE =
"create table notes (_id integer primary key autoincrement, "
+ "title text not null, body text not null);";

To

private static final String DATABASE_CREATE =
"create table notes (_id integer primary key autoincrement, "
+ "name text not null, body text not null);";

I also tend to just use the statics themselves in the create statement, so it could be written like the following:

    private static final String DATABASE_CREATE =
"create table notes (" + KEY_ROWID + " integer primary key autoincrement, "
+ KEY_NAME + " text not null, " + KEY_BODY + " text not null);";

Then you could change the names often and not encounter database create breakage.

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

1 Comment

This is exactly what I did. Still it crashes. I am stumped.
0

I guess problem is .. you are using KEY_NAME = "name"... Now as far as what I can see in your code that you are not creating a table in which you have 'name' coloumn. Now again might be you are trying to access value of name column which doesn't actually der in the table so it's throwing exception. But still a look to the Logcat (if you post here) can provide better answers to the problem.

Cheers!

2 Comments

Ok. I got the issue. I uninstalled the application from my phone and I reinstalled it. And it works now. Any ideas why this works?
Probably the application has already created a table with different coloumn. So when you were accessing that table it was throwing error. When you removed and reinstalled .. it just created a new table ...with the coloums you wanted. I guess this could be the reason

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.