我正在创建一个管理配料的应用程序。我想创建一个SQLite数据库,该数据库将保存用户单击“保存”按钮时输入的所有成分。但是,由于某种原因,我的数据无法保存到SQLite数据库。我根本看不出代码有什么问题。
public class DatabaseHelper extends SQLiteOpenHelper
{
private static final String tag = "Database Helper";
private static final String TABLE_NAME = "products_registered";
private static final String col1 = "Name";
private static final String col2 = "Weight";
private static final String col3 = "Price";
private static final String col4 = "Description";
@Override
public void onCreate(SQLiteDatabase db)
{
String createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
col1 + " TEXT NOT NULL," +
col2 + " DOUBLE NOT NULL," +
col3 + " DOUBLE NOT NULL," +
col4 + " TEXT);";
db.execSQL(createTable);
}
public DatabaseHelper(Context context)
{
super(context, TABLE_NAME, null, 1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int j)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item1, String item2, String item3, String item4)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col1, item1);
contentValues.put(col2, item2);
contentValues.put(col3, item3);
contentValues.put(col4, item4);
Log.d(tag,"Adding name " + item1 + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
db.close();
if(result == -1)
{
return false;
}
else
{
return true;
}
}
}
public class RegisterProduct extends AppCompatActivity
{
DatabaseHelper databaseHelper;
private Button saveProductButton;
private EditText productName;
private EditText productWeight;
private EditText productPrice;
private EditText productDescription;
DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_product);
saveProductButton = (Button) findViewById(R.id.saveButton);
productName = (EditText) findViewById(R.id.enterName);
productWeight = (EditText) findViewById(R.id.enterWeight);
productPrice = (EditText) findViewById(R.id.enterPrice);
productDescription = (EditText) findViewById(R.id.enterDescription);
mDatabaseHelper = new DatabaseHelper(this);
try
{
saveProductButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
String nameEntered = productName.getText().toString();
String weightEntered = productWeight.getText().toString();
String priceEntered = productPrice.getText().toString();
String descriptionEntered = productDescription.getText().toString();
addData(nameEntered, weightEntered, priceEntered, descriptionEntered);
productName.setText("");
}
});
}
finally
{
mDatabaseHelper.close();
}
}
public void addData(String newEntry1, String newEntry2, String newEntry3, String newEntry4)
{
mDatabaseHelper.addData(newEntry1, newEntry2, newEntry3, newEntry4);
}
我创建了一个单独的类来创建数据库和表本身,然后在创建活动的地方调用了将数据输入保存在下面显示的活动中的方法。
答案 0 :(得分:2)
您的问题很可能是您自运行应用以来更改了数据库的结构/架构,可能添加了一个列。
通常,在运行应用程序时,您将得到一个SQLite异常,指示未找到该列。但是,由于您已将 addData 包装在try / finally结构中,因此该异常已被抑制,因此看来一切正常,因为该应用不会崩溃或在日志中不显示任何内容。
将数据库代码包装在try构造中并捕获错误,如您所知,这可能会造成混淆,建议您不要这样做。
之所以更改 onCreate 方法的代码中的架构/结构(可能是问题的根本原因)的原因是, onCreate 方法仅自动运行创建数据库时。
如果更改了数据库的架构/结构,则您需要某种方式来强制进行结构更改,或者在开发应用程序时,由于不需要保存基础数据,因此最简单的方法是删除数据库,然后重新运行该应用程序。
可以通过删除应用程序的数据或通过卸载应用程序来删除数据库(请注意,任何现有数据都将丢失)。
另一种选择是删除相应的表并重新创建它们。通常会编写 onUpgrade 方法来执行此操作。如果是这种情况(您的情况),那么更改结构的另一种方法是通过增加传递给SQLiteOpenHelper构造函数的版本号来触发onUpgrage方法(超级调用)。您可以将super(context, TABLE_NAME, null, 2);
更改为{{1}}
删除数据库或增加版本号(最好从调用 addData 的周围删除try / final结构)并重新运行应用程序后,它可能就可以正常工作。如果不是,则日志应显示任何异常,然后可以查明问题。