开发者

Basic troubles with SQLite databases. Notepad tutorial of android

开发者 https://www.devze.com 2023-03-20 16:20 出处:网络
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 KE

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);"; 开发者_如何转开发*/


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号