Android 5.0 SQLITE 插入失败
Android 5.0 SQLITE insert fail
每次我尝试在数据库中插入数据时,我都会遇到一个奇怪的错误:
Error inserting date=1439183960 amount=5.0 icon=1 description=g or_amount=0.0 lat=0.0 long=0.0
android.database.sqlite.SQLiteException: table transactions has no column named icon (code 1): ,
while compiling: INSERT INTO transactions(date,amount,icon,description,or_amount,lat,long) VALUES (?,?,?,?,?,?,?)
错误表明 table icon
没有列 icon
这是我创建的查询:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS transactions (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"amount REAL, " +
"or_amount REAL, " +
"description TEXT" +
"icon INTEGER, " +
"lat REAL, " +
"long REAL, " +
"date INTEGER" +
")");
}
这是将一些实体插入数据库的方法:
public long insert (Entitie entitie) {
long NO_INSERTED_ENTITIE = -1;
long id = NO_INSERTED_ENTITIE;
getWritableDb();
if (entitie.getId() == NO_INSERTED_ENTITIE){
id = db.insert(entitie.getTableName(), null, entitie.getContentValues());
entitie.setId( (int) id );
}
return id;
}
这里是实体,它被称为 PaymentLog
:
public class PaymentLog extends Entitie{
//====== tables fields======
private static final String tableName = "transactions";
private static final String[] columnNames = {"id", "amount", "or_amount", "description", "icon", "long", "lat", "date"};
private final int ID_POSITION = 0, AMOUNT_POSITION =1, OR_AMOUNT_POSITION = 2, ICON_POSTION = 4, LONG_POSTION =5, LAT_POSTION=6, DATE_POSTION=7, DESCRIPTION_POSTION = 3;
@Override
public ContentValues getContentValues(){
ContentValues content = new ContentValues();
content.put( columnNames[ AMOUNT_POSITION ], this.getTransaction());
content.put( columnNames[ OR_AMOUNT_POSITION ], this.getOriginal_amount());
content.put( columnNames[ ICON_POSTION ], this.getIcon() );
content.put( columnNames[ LONG_POSTION ], this.getLang() );
content.put( columnNames[ LAT_POSTION ], this.getLat() );
content.put( columnNames[ DATE_POSTION ], this.getDate().getTime()/1000 );
content.put( columnNames[ DESCRIPTION_POSTION ], this.getDescription() );
return content;
}
public String getTableName() {
return tableName;
}
}
我不知道为什么会出现此错误,我的创建查询是正确的,我从 phone 中清除了缓存和应用程序数据,以使用 onCreate
方法重建数据库。
我的 class PaymentLog
它也是正确的,我不知道它有什么问题,我听说 Android 5.0 上的 sqlite
有一些变化,但我不知道是否插入有变化。
问题出在您的创建 table 语句
you haven't added comma after description Text
所以试试
db.execSQL("CREATE TABLE IF NOT EXISTS transactions (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"amount REAL, " +
"or_amount REAL, " +
"description TEXT, " +
"icon INTEGER, " +
"lat REAL, " +
"long REAL, " +
"date INTEGER" +
")");
您需要仔细查看查询中的逗号和空格,否则您的查询将无法 运行 正确。
每次我尝试在数据库中插入数据时,我都会遇到一个奇怪的错误:
Error inserting date=1439183960 amount=5.0 icon=1 description=g or_amount=0.0 lat=0.0 long=0.0
android.database.sqlite.SQLiteException: table transactions has no column named icon (code 1): ,
while compiling: INSERT INTO transactions(date,amount,icon,description,or_amount,lat,long) VALUES (?,?,?,?,?,?,?)
错误表明 table icon
没有列 icon
这是我创建的查询:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS transactions (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"amount REAL, " +
"or_amount REAL, " +
"description TEXT" +
"icon INTEGER, " +
"lat REAL, " +
"long REAL, " +
"date INTEGER" +
")");
}
这是将一些实体插入数据库的方法:
public long insert (Entitie entitie) {
long NO_INSERTED_ENTITIE = -1;
long id = NO_INSERTED_ENTITIE;
getWritableDb();
if (entitie.getId() == NO_INSERTED_ENTITIE){
id = db.insert(entitie.getTableName(), null, entitie.getContentValues());
entitie.setId( (int) id );
}
return id;
}
这里是实体,它被称为 PaymentLog
:
public class PaymentLog extends Entitie{
//====== tables fields======
private static final String tableName = "transactions";
private static final String[] columnNames = {"id", "amount", "or_amount", "description", "icon", "long", "lat", "date"};
private final int ID_POSITION = 0, AMOUNT_POSITION =1, OR_AMOUNT_POSITION = 2, ICON_POSTION = 4, LONG_POSTION =5, LAT_POSTION=6, DATE_POSTION=7, DESCRIPTION_POSTION = 3;
@Override
public ContentValues getContentValues(){
ContentValues content = new ContentValues();
content.put( columnNames[ AMOUNT_POSITION ], this.getTransaction());
content.put( columnNames[ OR_AMOUNT_POSITION ], this.getOriginal_amount());
content.put( columnNames[ ICON_POSTION ], this.getIcon() );
content.put( columnNames[ LONG_POSTION ], this.getLang() );
content.put( columnNames[ LAT_POSTION ], this.getLat() );
content.put( columnNames[ DATE_POSTION ], this.getDate().getTime()/1000 );
content.put( columnNames[ DESCRIPTION_POSTION ], this.getDescription() );
return content;
}
public String getTableName() {
return tableName;
}
}
我不知道为什么会出现此错误,我的创建查询是正确的,我从 phone 中清除了缓存和应用程序数据,以使用 onCreate
方法重建数据库。
我的 class PaymentLog
它也是正确的,我不知道它有什么问题,我听说 Android 5.0 上的 sqlite
有一些变化,但我不知道是否插入有变化。
问题出在您的创建 table 语句
you haven't added comma after description Text
所以试试
db.execSQL("CREATE TABLE IF NOT EXISTS transactions (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"amount REAL, " +
"or_amount REAL, " +
"description TEXT, " +
"icon INTEGER, " +
"lat REAL, " +
"long REAL, " +
"date INTEGER" +
")");
您需要仔细查看查询中的逗号和空格,否则您的查询将无法 运行 正确。