【问题标题】:Android SQLite not inserting in the order I tell it toAndroid SQLite 没有按照我告诉它的顺序插入
【发布时间】:2015-02-05 12:03:24
【问题描述】:

我有一个对象,它被调用来向数据库添加一行,但由于我无法弄清楚的原因,它试图不按我告诉它的顺序插入它。

这是方法的代码

public Coin createCoin(String create_year, String create_specialty, String create_mintage,  String create_mint, int create_have) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.MINT, create_mint);
    values.put(MySQLiteHelper.YEAR, create_year);
    values.put(MySQLiteHelper.MINTAGE, create_mintage);
    values.put(MySQLiteHelper.HAVE, create_have);
    values.put(MySQLiteHelper.SPECIALTY, create_specialty);
    //(String table, String nullColumnHack, ContentValues values)
    long insertId = database.insert(MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE, null,
            values);
    //(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
    Cursor cursor = database.query(MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE,
            allColumns, MySQLiteHelper.YEAR, null,
            null, null, null);
    cursor.moveToFirst();
    Coin newCoin = cursorToCoin(cursor);
    cursor.close();
    return newCoin;
}
public ArrayList<Coin> getAllCoins() {
    ArrayList<Coin> coins = new ArrayList<Coin>();
    Cursor cursor = database.query(MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE,
            allColumns, null, null, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Coin coin = cursorToCoin(cursor);
        coins.add(coin);
        cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return coins;
}
private Coin cursorToCoin(Cursor cursor) {
    Coin coin = new Coin();
    coin.setYear(cursor.getString(0));
    coin.setSpecialty(cursor.getString(1));
    coin.setMintage(cursor.getString(2));
    return coin;
}

现在我认为它会尝试将数据插入为

INSERT INTO penny_flying_eagle(mint,year,mintage,have,specialty) VALUES (?,?,?,?,?)

但它却给了我一个错误

66-1166/com.example.dommol.testlist E/SQLiteLog﹕ (1) table penny_flying_eagle has no column named mint
   02-03 21:31:42.097    1166-1166/com.example.dimmel.testlist E/SQLiteDatabase﹕ Error inserting mint=0 year=1858 mintage=24,600,000 specialty=Not NULL have=D
android.database.sqlite.SQLiteException: table penny_flying_eagle has no column named mint (code 1): , while compiling: INSERT INTO penny_flying_eagle(mint,year,mintage,specialty,have) VALUES (?,?,?,?,?)

我的想法是它告诉我there is no column named mint,因为它试图将mint 放在year 的位置并且没有在那里找到该列。为什么这试图以与我指定的顺序不同的顺序插入值?

编辑:创建表的代码

public class MySQLiteHelper extends SQLiteOpenHelper {

public static final String TABLE_PENNY_FLYING_EAGLE = "penny_flying_eagle";
public static final String YEAR = "year";
public static final String SPECIALTY = "specialty";
public static final String MINTAGE = "mintage";
public static final String HAVE = "have";
public static final String MINT = "mint";

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

// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
        + TABLE_PENNY_FLYING_EAGLE + "("
        + MINT + " text, "
        + YEAR + " text, "
        + MINTAGE + " text not null, "
        + SPECIALTY + " text, "
        + HAVE + " integer, "
        + "primary key (" + YEAR + ", " + SPECIALTY + ", " + MINTAGE + ") on conflict ignore );";

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);

【问题讨论】:

  • 可能是它的敏感案例。我想把你的薄荷换成薄荷
  • 你能告诉我们你的sql来创建那个表吗?
  • 有创建表的函数。这不是大小写问题,它从创建表的同一个类中提取列的名称
  • 您是否通过将 db 从设备中拉出并查看它来检查数据库中是否确实存在 mint 列?不要假设您的工具会错误地告诉您
  • 我不知道该行怎么会不存在。 create 语句使用与 insert 语句完全相同的名称。

标签: android sqlite


【解决方案1】:

这个问题有两个原因:

  1. 如果您重命名了列名。 (即 mint 列),因为一旦创建数据库,更改列名时不会更改表。

  2. 如果您为已经存在的表添加了新的列名。(即 mint 列)

解决方案:

  1. 将 DATABASE_VERSION 从 1 更改(升级)到 2 等等。

  2. 从 data/data/"your app name"/databases/"your database" 中删除数据库并运行应用程序,以便创建新数据库。

【讨论】:

  • 我是在模拟器上运行的,是不是应该每次运行或者重启模拟器时都重新创建并重新填充数据库?
  • m 不是说你每次运行项目时都要重新创建数据库 m 只是说你,如果你改变了表然后重新创建 db 或者只是将数据库版本从 1 更改为 2 或任何你希望。
  • 我认为是误会了,当我关闭模拟器时不会删除数据库吗?问题不可能是我试图向现有数据库添加新列,因为不应该存在现有数据库
  • 是的,你是对的,所以我告诉你手动删除数据库或更改(升级)数据库版本。
  • 关闭模拟器不会删除数据库。您需要自己删除它(通过卸载应用程序或在代码中编写一些内容)。您应该将模拟器视为真实设备,它将保留您在其中安装的所有内容。
猜你喜欢
  • 2011-11-12
  • 1970-01-01
  • 2012-06-04
  • 2015-04-12
  • 1970-01-01
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多