我试图将新项目添加到我的数据库中,并且我收到此错误消息。我已经尝试了所有我能想到的东西。有什么我搞砸了吗?
05-04 23:46:28.425: E/AndroidRuntime(1378): FATAL EXCEPTION: main
05-04 23:46:28.425: E/AndroidRuntime(1378): Process: com.nmass.makeupmethodology, PID: 1378
05-04 23:46:28.425: E/AndroidRuntime(1378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nmass.makeupmethodology/com.nmass.makeupmethodology.MainActivity}: android.database.sqlite.SQLiteException: no such column: Product_Name (code 1): , while compiling: SELECT _id, Product_Name, Type, Color, Brand, Pallet_Name, Prod_Pic, Swatch_Pic, Date_Purchased, Misc FROM Products
05-04 23:46:28.425: E/AndroidRuntime(1378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-04 23:46:28.425: E/AndroidRuntime(1378): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-04 23:46:28.425: E/AndroidRuntime(1378): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-04 23:46:28.425: E/AndroidRuntime(1378): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-04 23:46:28.425: E/AndroidRuntime(1378): at android.os.Handler.dispatchMessage(Handler.java:102)
05-04 23:46:28.425: E/AndroidRuntime(1378): at android.os.Looper.loop(Looper.java:136)
05-04 23:46:28.425: E/AndroidRuntime(1378): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-04 23:46:28.425: E/AndroidRuntime(1378): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 23:46:28.425: E/AndroidRuntime(1378): at java.lang.reflect.Method.invoke(Method.java:515)
05-04 23:46:28.425: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-04 23:46:28.425: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-04 23:46:28.425: E/AndroidRuntime(1378): at dalvik.system.NativeStart.main(Native Method)
05-04 23:46:28.425: E/AndroidRuntime(1378): Caused by: android.database.sqlite.SQLiteException: no such column: Product_Name (code 1): , while compiling: SELECT _id, Product_Name, Type, Color, Brand, Pallet_Name, Prod_Pic, Swatch_Pic, Date_Purchased, Misc FROM Products
05-04 23:46:28.425: E/AndroidRuntime(1378): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
这是实际提交数据并从editTexts和东西中获取数据的onClick方法。
public void onSubmitClick(View view){
productName = (EditText)findViewById(R.string.AddNew_editTextNameName);
productPalletName = (EditText)findViewById(R.string.AddNew_editTextPalletName);
productBrandName = (EditText)findViewById(R.string.AddNew_editTextBrandName);
productDatePurchased = (EditText)findViewById(R.string.AddNew_editViewDatePurchasedName);
productMisc = (EditText)findViewById(R.string.AddNew_editViewOtherName);
productType = (Spinner)findViewById(R.string.AddNew_spinnerTypeName);
productColor = (Spinner)findViewById(R.string.AddNew_spinnerColorCatName);
ProductsDataSource datasource = new ProductsDataSource(this);
datasource.open();
if(isPallet){
datasource.createProduct(productName.getText().toString(), productType.getId(), productColor.getId(),
productBrandName.getText().toString(), productPalletName.getText().toString(), prodPath, swatchPath,
productDatePurchased.getText().toString(), productMisc.getText().toString());
datasource.close();
Toast.makeText(this, "Creating New Product In Database", Toast.LENGTH_SHORT).show();
} else{
datasource.createProduct(productName.getText().toString(), productType.getSelectedItemPosition(), productColor.getSelectedItemPosition(),
productBrandName.getText().toString(), null, prodPath, swatchPath,
productDatePurchased.getText().toString(), productMisc.getText().toString());
datasource.close();
Toast.makeText(this, "Creating New Product In Database", Toast.LENGTH_SHORT).show();
}
productListActivity();
finish();
}
这是实际创建项目的方法。我使用database.insert,我创建了SQL插件,试图解决我的问题。
public void createProduct(String productName, int productType, int productColor, String productBrand,
String productPalletName, String productProdPic, String productSwatchPic, String productDateOpened, String productMisc){
ContentValues productValues = new ContentValues();
//productValues.putNull(SQLiteHelper.colID);; //0 - written for order ease
productValues.put(SQLiteHelper.colName, productName); //1
productValues.put(SQLiteHelper.colType, productType);//2
productValues.put(SQLiteHelper.colPalletName, productPalletName); //3
productValues.put(SQLiteHelper.colBrand, productBrand); //4
productValues.put(SQLiteHelper.colColor,productColor);//5
productValues.put(SQLiteHelper.colProdPic, productProdPic);//6
productValues.put(SQLiteHelper.colSwatchPic, productSwatchPic); //7
productValues.put(SQLiteHelper.colDate, productDateOpened); //8
productValues.put(SQLiteHelper.colMisc, productMisc); //9
// Inserting Row
database.insert(productTable, null, productValues);
database.close(); // Closing database connection
/* database.execSQL("INSERT INTO "+ productTable + " (" + SQLiteHelper.colName + ","
+ SQLiteHelper.colType + "," + SQLiteHelper.colColor + "," + SQLiteHelper.colBrand +
"," + SQLiteHelper.colPalletName + "," + SQLiteHelper.colProdPic + "," + SQLiteHelper.colSwatchPic
+ "," + SQLiteHelper.colDate + "," + SQLiteHelper.colMisc + ") VALUES ( '" + productName + "' , " + productType
+ " , " + productColor + " ,'" + productBrand + "' , '" + productPalletName + "' , '" + productProdPic + "','" +
productSwatchPic + "'," + productDateOpened + ",'" + productMisc + "');");
database.close(); */
}
以下是它引用的另一种方法 - 它为列表视图提供了项目列表
public Cursor getAllProduct() {
List<Product> productsList = new ArrayList<Product>();
Cursor cursor = database.query(SQLiteHelper.productTable,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Product product = cursorToProduct(cursor);
productsList.add(product);
cursor.moveToNext();
cursor.close();
}
return cursor;
}
如果你想要它,这里是表创建
公共类SQLiteHelper扩展了SQLiteOpenHelper {
static final String dbName="MakeupMethodology";
static final String productTable="Products";
static final String colID="_id";
static final String colName="Product_Name";
static final String colType= "Type";
static final String colPalletName = "Pallet_Name";
static final String colBrand="Brand";
static final String colColor="Color";
static final String colProdPic="Prod_Pic";
static final String colSwatchPic = "Swatch_Pic";
static final String colDate = "Date_Purchased";
static final String colMisc = "Misc";
static final String CreateDatabase =
"CREATE TABLE IF NOT EXISTS " + productTable +" (" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT , "+ colName + " TEXT NOT NULL , "
+ colType + " TEXT NOT NULL, " + colPalletName + " TEXT , "+ colBrand + " TEXT NOT NULL, " + colColor + " TEXT NOT NULL, "
+ colProdPic + " TEXT , " + colSwatchPic + " TEXT , " + colDate + " DATE NOT NULL , " + colMisc + " TEXT)";
private static final int DATABASE_VERSION = 1;
编辑:我在某处找到了(初学者:()拼写错误以及我是如何得到新的错误消息。它已经更新了旧版本的地方
编辑:我再过几个小时就无法回答我自己的问题,所以我只是把它放在这里以防万一有人遇到类似的问题:如果您的数据库表现得很奇怪并且您在模拟器上,则清理工作区并重新启动模拟器是不够的。删除整个过程并从SCRATCH开始(如果您已将sdcard图像附加到模拟器上,则可以保留它,只需从配置菜单中删除模拟器并创建一个新模拟器)
答案 0 :(得分:0)
日志说明了一切:没有名为Product_Name
的列。这应该告诉你:
检查它是否真的存在。如果您使用sqlite在模拟器或root设备上进行测试,请尝试在shell上运行.schema Products
如果您直接从eclipse安装,请确保每次运行应用时都更新数据库版本。除非应用检测到数据库版本已升级,否则表定义不会反映出来。