我已经在堆栈溢出中找到了与此问题相关的所有帖子,但无法获得任何解决方案。我在android中使用sqlite数据库存储数据,但每次我尝试它都会抛出一个表没有这样的列的错误。
//数据库类的代码
public class demo extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "orderTimeManager";
// Contacts table name
private static final String TABLE_ORDER = "orderTime";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String PRODUCT_ID = "product_id";
private static final String PRODUCT_NAME = "product_name";
private static final String PRODUCT_DETAIL = "product_detail";
private static final String PRODUCT_TYPE = "product_type";
private static final String PRODUCT_PRICE = "product_price";
public demo(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_ORDER_TABLE = "CREATE TABLE " + TABLE_ORDER + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ PRODUCT_NAME + " TEXT," + PRODUCT_DETAIL + " TEXT,"
+ PRODUCT_TYPE + " TEXT,"
+ PRODUCT_PRICE + " TEXT" + ")";
db.execSQL(CREATE_ORDER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORDER);
// Create tables again
onCreate(db);
}
// Adding new order
public void addOrder(ProductModel object) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
// Contact Name
values.put(PRODUCT_NAME, object.getProductName()); // Contact Phone Number
values.put(PRODUCT_DETAIL, object.getProductDetail());
values.put(PRODUCT_TYPE, object.getProductType());
values.put(PRODUCT_PRICE, object.getProductType());
// Inserting Row
db.insert(TABLE_ORDER, null, values);
db.close(); // Closing database connection
}
// Getting All order
public List<ProductModel> getAllOrders() {
List<ProductModel> orderList = new ArrayList<ProductModel>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_ORDER;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
ProductModel object = new ProductModel();
object.setProductId(cursor.getString(0));
object.setProductName((cursor.getString(1)));
object.setProductDetail(cursor.getString(2));
object.setProductType(cursor.getString(3));
object.setProductPrice(Integer.parseInt(cursor.getString(4)));
// Adding contact to list
orderList.add(object);
} while (cursor.moveToNext());
}
// return contact list
return orderList;
}
//在另一个类中调用数据库来添加数据
JSONObject ob1 = jsonArray.getJSONObject(i);
{
String productId = ob1.getString("pkproductId");
String productName = ob1.getString("name");
String productType = ob1.getString("productType");
String productPrice = ob1.getString("price");
String productDetails = ob1.getString("details");
Log.d(TAG, "Inserting");
db.addOrder(new ProductModel(productName,productDetails,productType,Integer.parseInt(productPrice)));
}
// Product Model
public ProductModel( String productName, String productDetail, String productType, int productPrice) {
this.productId = productId;
this.productName = productName;
this.productDetail = productDetail;
this.productPrice = productPrice;
this.productType = productType;
}
答案 0 :(得分:1)
一步一步尝试
1)尝试更改DATABASE_VERSION = 1;到2:然后重新安装
2)在ProductModel中使用String productPrice并从Integer.parseInt(
中删除db.addOrder
。因为您的数据库具有TEXT数据类型
3)改变这一行
values.put(PRODUCT_TYPE, object.getProductType());
values.put(PRODUCT_PRICE, object.getProductPrice());
希望它对你有所帮助。如果这没有帮助随意提出问题