数据库Android卡住了

时间:2014-04-07 11:11:15

标签: android sqlite

我的数据库有点问题。我在网站上跟随了一个例子,创建了处理数据库所需的一切。不幸的是,它不想工作。我有文本的错误消息:表格gifs没有列名为rating,同时尝试将对象插入该表。我理解这个消息,但我真的不明白为什么一切都应该正常。这是简单的数据库,现在应该添加对象。如果在创建数据库时创建该列,为什么找不到该列?

以下是MAINACTIVITY的代码:

 dataBaseManager.insertGifToDataBase(tmpGif);

其中tmpGif包含字段:名称,日期,评级和网址。除了评级,其余的是字符串。

这是我的SQLiteHelper和Class wchich管理数据库:

public class SqliteHelper extends SQLiteOpenHelper{
public static final String TABLE_NAME = "gifs";
  public static final String NAME = "name";
  public static final String RATING = "rating";
  public static final String DATE = "date";
  public static final String URL = "url";

  private static final String DATABASE_NAME = "gifs.db";
  private static final int DATABASE_VERSION = 1;

  private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + NAME + "text" + "," + RATING + "int" + "," + URL + "text" + "," + DATE + "text" + ");";

public SqliteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    //Log.i("DATA BASE", getDatabaseName());
  }

@Override
public void onCreate(SQLiteDatabase database) {

     database.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {

    database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(database);

}
}

3 个答案:

答案 0 :(得分:2)

在create table query中缺少列名和数据类型之间的空格以获取正确的结构:

 private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + NAME + "text" + "," + RATING + "int" + "," + URL + "text" + "," + DATE + "text" + ");";  

应该是:

 private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + NAME + " text" + "," + RATING + " int" + "," + URL + " text" + "," + DATE + " text" + ");";  

因此,表格未正确创建,无法找到列。

答案 1 :(得分:1)

您需要在列名称及其类型之间留出空格:

private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + NAME + " text" + "," + RATING + " int" + "," + URL + " text" + "," + DATE + " text" + ");";

所以,例如评分为rating int而不是ratingint

更新SQL后,卸载您的应用,以便删除旧的数据库文件。

答案 2 :(得分:1)

您的DATABASE_CREATE

需要稍作修改

请按以下方式进行

private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + NAME + " text" + "," + RATING + " int" + "," + URL + " text" + "," + DATE + " text" + ");";