我有此代码:
{
String SQL_CREATE_BOOKS_TABLE = "CREATE TABLE " + BooksContract.BooksEntry.TABLE_NAME + " ("
+ BooksContract.BooksEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_PRODUCT + " TEXT NOT NULL, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_PRICE + " DECIMAL NOT NULL DEFAULT 0, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY + " INTEGER NOT NULL, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_SUPPLIER + " TEXT, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_PHONE + " INTEGER );";
db.execSQL(SQL_CREATE_BOOKS_TABLE);
}
即使我没有将供应商设置为not null
,但是如果我在单击“保存”按钮时未在editText
中键入供应商,应用也会崩溃。另外,即使数量设置为默认值0,如果我不输入任何数量,它仍然会崩溃。为什么?
EditorActivity:
private void insertBooks() {
String productString = productName.getText().toString().trim();
String priceString = price.getText().toString().trim();
int price = Integer.parseInt(priceString);
String quantityString = quantity.getText().toString().trim();
int quantity = Integer.parseInt(quantityString);
String supplierString = supplier.getText().toString().trim();
String phoneString = supplierPhone.getText().toString().trim();
int phone = Integer.parseInt(phoneString);
BooksDbHelper dbHelper = new BooksDbHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRODUCT, productString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRICE, priceString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY, quantityString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_SUPPLIER, supplierString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PHONE, phoneString);
long newRowId = db.insert(BooksContract.BooksEntry.TABLE_NAME, null, values);
if (newRowId == -1) {
// If the row ID is -1, then there was an error with insertion.
Toast.makeText(this, "Error with saving book", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Book saved with row id: " + newRowId, Toast.LENGTH_SHORT).show();
}
}
CatalogActivity:
private void insertBooks() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRODUCT, "Walks with men");
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRICE, 10.00);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY, 2);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_SUPPLIER, "Amazon");
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PHONE, 727213658);
long newRowId = db.insert(BooksContract.BooksEntry.TABLE_NAME, null, values);
}
答案 0 :(得分:0)
在您的insertBooks()
方法中使用此代码可避免使用NumberFormatException
:
String productString = productName.getText().toString().trim();
String priceString = price.getText().toString().trim();
double price = 0.0;
try {
price = Double.parseDouble(priceString);
} catch (NumberFormatException e) {
e.printStackTrace();
}
String quantityString = quantity.getText().toString().trim();
int quantity = 0;
try {
quantity = Integer.parseInt(quantityString);
} catch (NumberFormatException e) {
e.printStackTrace();
}
String supplierString = supplier.getText().toString().trim();
String phoneString = supplierPhone.getText().toString().trim();
为什么电话是INTEGER?
注意:
您发布的创建表代码可能是扩展onCreate()
的类中SQLiteOpenHelper
方法的一部分。
onCreate()
方法是您第一次运行应用程序并创建表时执行的方法。
从那时起,您可能已按类型或名称更改了列,甚至更改了插入或删除的列,但所有这些更改都是仅在您的代码中而不在表中进行的。 onCreate()方法再也不会触发。
因此,如果以上任何一项适用于您:
从模拟器/设备上卸载应用程序,以便删除数据库,然后再次运行您的应用程序以按原样重新创建它。