错误插入值数据库 [英] Error in Inserting values to Database
问题描述
我创建一个简单的数据库增加产品的价值。而在数据库中添加条目我得到的一个logcat中错误并计划得到停在那里,然后。
I am creating a simple Database to add the values of product. While adding the entries in database I am getting an error in Logcat and the program get stop there and then.
我不与相关数据的插入或查询我已经写了错误,但它的东西清楚。我尝试了所有可能的选择我可以。
I am not clear with the error but its something related to insertion of data or in query I have written. I tried all possible alternatives I could.
程序code是:
DataBase.java
public class DataBase extends SQLiteOpenHelper
{
public DataBase(Context context) {
super(context, CreateTable.DB_NAME, null, CreateTable.DB_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String create = "Create Table " + CreateTable.TABLE_NAME + "( " + CreateTable.KEY_ID
+ " INTEGER PRIMARY KEY," + CreateTable.KEY_NAME + " TEXT,"
+ CreateTable.KEY_PRICE + " REAL)";
db.execSQL(create);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void addProduct(Product p)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues c = new ContentValues();
c.put(CreateTable.KEY_NAME, p.getName());
c.put(CreateTable.KEY_PRICE, p.getPrice());
db.insert(CreateTable.TABLE_NAME, null, c);
db.close();
}
}
EnterDeatils.java
public class EnterDeatils extends Activity {
EditText name;
EditText price;
Button done;
int id = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_deatils);
name = (EditText) findViewById(R.id.edtname);
price = (EditText) findViewById(R.id.edtprice);
done = (Button) findViewById(R.id.btndone);
done.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Product p = new Product();
p.setId(id);
p.setName(name.getText().toString());
p.setPrice(Float.valueOf(price.getText().toString()));
DataBase d = new DataBase(EnterDeatils.this);
d.addProduct(p);
}
});
}
}
LogCat中错误:
01-12 23:06:52.343: E/Database(382): Error inserting pPrice=12.0 pName=ds
01-12 23:06:52.343: E/Database(382): android.database.sqlite.SQLiteException: table Books has no column named pPrice: , while compiling: INSERT INTO Books(pPrice, pName) VALUES(?, ?);
请求你们刚刚帮我识别错误。
先谢谢了。
Requesting you guys to just help me to identify the error. Thanks in Advance.
推荐答案
SQLiteOpenHelper
的onCreate()
只调用如果数据库文件不存在。如果你修改SQL 的onCreate()
,你必须确保数据库文件被更新。
SQLiteOpenHelper
onCreate()
is only called if the database file does not exist. If you modify the SQL in onCreate()
, you'll have to ensure the database file is updated.
两种方法:
-
删除旧版本的数据库。卸载做到这一点的方法之一。通过这种方式,数据库与任何code创建您目前在
的onCreate()
。这通常是应用程序开发过程中最简单的方法。
Delete the old version of the database. Uninstall is one way to do this. This way the database is created with whatever code you currently have in
onCreate()
. This is often the simplest way during app development.
撞了你传递给 SQLiteOpenHelper
父数据库的版本号。如果这个数字是从存储在数据库文件的版本号不同, onUpgrade()
或 onDowngrade()
被称为,你可以更新数据库架构。这是preferred的方式,当你已经发布的版本出来,以便用户可以更新您的应用程序时preserve他们的数据。
Bump up the database version number you pass to SQLiteOpenHelper
superclass. If this number is different from the version number stored in the database file, onUpgrade()
or onDowngrade()
is called, and you can update the database schema. This is the preferred way when you already have released versions out so your users can preserve their data when updating your app.
这篇关于错误插入值数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!