首页 文章

SQLite数据库的基本问题 . android的记事本教程

提问于
浏览
0

这是一个非常基本的问题 . 我只是想了解SQLite数据库是如何工作的 . 所以,这就是我所做的:在下面的代码部分取自记事本教程第3练习,我将KEY_TITLE更改为KEY_NAME,所有地方我都找到了名称的 Headers . 应用程序崩溃了 . 为什么会这样?

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);";

/ *更改为:private static final String DATABASE_CREATE =“create table notes(_id integer primary key autoincrement,”“name text not null,body text not null);”; * /

2 回答

  • 0

    如果您复制堆栈跟踪(使用logcat / DDMS)或复制整个SQLiteDBAdapter会很有帮助,但只是查看您发布的内容,您肯定会遇到一个问题,即您在sqlite数据库中使用了错误的字段名称声明 .

    “title”字段应重命名为“name”以匹配您更改的列名 .

    更改:

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

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

    我也倾向于在create语句中使用静态,因此它可以写成如下所示:

    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);";
    

    然后你可以经常更改名称而不会遇到数据库创建破坏 .

  • 1

    我想问题是......你正在使用KEY_NAME =“name”...现在我在你的代码中看到的是你没有创建一个你有'name'coloumn的表 . 现在再次可能是您正在尝试访问名称列的值,该值实际上不在表中,因此它会抛出异常 . 但是看看Logcat(如果你在这里发布)可以提供更好的问题答案 .

    干杯!

相关问题