【发布时间】: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 语句完全相同的名称。