Android SQLite 没有按照我告诉它的顺序插入
Android SQLite not inserting in the order I tell it to
我有一个对象被调用以向数据库中添加一行,但由于我无法弄清楚的原因,它试图不按照我告诉它的顺序插入它。
这是方法的代码
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
的位置并且没有找到那里的列。为什么这会尝试以与我指定的顺序不同的顺序插入值?
编辑:创建代码 table
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);
这个问题是由两件事引起的:
如果你重命名了列名。 (即 mint 列)因为一旦创建数据库,它不会在更改列名时更改 table。
如果您为已经存在的 table 添加了新的列名称。(即新列)
解决方案:
将 DATABASE_VERSION 从 1 更改(升级)为 2 等。
从 data/data/"您的应用程序名称"/databases/"您的数据库" 和 运行 应用程序中删除数据库,以便创建新数据库。
我有一个对象被调用以向数据库中添加一行,但由于我无法弄清楚的原因,它试图不按照我告诉它的顺序插入它。
这是方法的代码
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
的位置并且没有找到那里的列。为什么这会尝试以与我指定的顺序不同的顺序插入值?
编辑:创建代码 table
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);
这个问题是由两件事引起的:
如果你重命名了列名。 (即 mint 列)因为一旦创建数据库,它不会在更改列名时更改 table。
如果您为已经存在的 table 添加了新的列名称。(即新列)
解决方案:
将 DATABASE_VERSION 从 1 更改(升级)为 2 等。
从 data/data/"您的应用程序名称"/databases/"您的数据库" 和 运行 应用程序中删除数据库,以便创建新数据库。