2014-05-05 99 views
1

我想添加一个新的项目到我的数据库,我收到此错误消息。我试过了我能想到的一切。有什么我搞砸了吗?SQLite错误,找不到我的语法错误。代码1

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(); */ 
    } 

这里是另一种方法,它是referencing-它与项目

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开始(如果你已经附加了一个SD卡图像到模拟器,你可以保留它,只需从配置菜单中删除模拟器,并创建一个新的)模拟器

+0

请上传'productName'文件的变量声明部分 – Kedarnath

+0

完成!我发现了一个错字,但现在我又收到了一条错误消息 - 我也更新了这个错误消息 – ReptarPrincess

+1

做一件事,就是删除你的应用程序并运行一个新版本。 – Kedarnath

回答

0

日志说这一切:没有列名为Product_Name。这应该告诉你:

  • 检查它是否真的存在。如果您正在使用sqlite在仿真器或根源设备上进行测试,请尝试在外壳上运行.schema Products

  • 确保您每次运行应用程序时都更新数据库版本,如果直接从eclipse安装。除非应用程序检测到数据库版本已升级,否则表格定义将不会反映出来。

相关问题