5

CSV からデータを挿入するときに問題があります。CSV 部分は機能しますが、挿入すると次のエラーが返されます。基本的には列が存在しないと言います。この下には、コードとデータベースのコードがあります。

09-06 17:52:22.725: E/AndroidRuntime(19168): FATAL EXCEPTION: main

09-06 17:52:22.725: E/AndroidRuntime(19168): android.database.sqlite.
SQLiteException: no such column: Ananas (code 1): , while compiling: 
INSERT INTO food (name, sacharides, glycemia, category1) VALUES    
(Ananas, 13, 45,1)

09-06 17:52:22.725: E/AndroidRuntime(19168):    
at android.database.sqlite.SQLiteConnection.nativePrepareStatement
(Native Method)

これが失敗するコードです。

            FoodDatabase myHelper = new FoodDatabase(getApplicationContext());
            myDatabase = myHelper.getReadableDatabase();

            String[] nextLine;
            try {
                while ((nextLine = reader.readNext()) != null) {
                    // nextLine[] is an array of values from the line
                    System.out.println(nextLine[0]+ " " + nextLine[1]
                            +" "+ nextLine[2]);

                    String sql =   "INSERT INTO food (name, sacharides, glycemia, category1) " +
                            "VALUES (" + nextLine[0] + ", " + nextLine[1] + ", "  + nextLine[2]+ "," + nextLine[3] +")";
                //  myDatabase.rawQuery(sql, null);
                    myDatabase.execSQL(sql);
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

データベースの外観は次のとおりです。

public class FoodDatabase extends SQLiteOpenHelper {

  public static final String TABLE_FOOD = "food";
  public static final String COLUMN_ID = "id";
  public static final String COLUMN_NAME = "name";
  public static final String COLUMN_SACHARIDES = "sacharides";
  public static final String COLUMN_SACHARIDESPORTION = "sacharides_per_portion";
  public static final String COLUMN_PORTIONSIZE = "portion_size";
  public static final String COLUMN_GLYCEMIA = "glycemia";
  public static final String COLUMN_CATEGORY1 = "category1";
  public static final String COLUMN_CATEGORY2 = "category2";
  public static final String COLUMN_CATEGORY3 = "category3";




  private static final String DATABASE_NAME = "tables.db";
  private static final int DATABASE_VERSION = 1;

  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
      + TABLE_FOOD + "(" + COLUMN_ID
      + " integer primary key autoincrement, " + COLUMN_NAME
      + " text," + COLUMN_SACHARIDES
      + " real," + COLUMN_PORTIONSIZE
      + " integer," + COLUMN_SACHARIDESPORTION
      + " real," + COLUMN_GLYCEMIA
      + " integer,"+ COLUMN_CATEGORY1
      + " integer," +COLUMN_CATEGORY2
      + " integer," +COLUMN_CATEGORY3
      + " integer);";

  public FoodDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(FoodDatabase.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_FOOD);
    onCreate(db);
  }
4

2 に答える 2

14

よくわかりませんが''、 内の文字列を一重引用符で囲む必要があるかもしれませんVALUES。試す:

String sql = "INSERT INTO food (name, sacharides, glycemia, category1) " +
                            "VALUES ('" + nextLine[0] + "', '" + nextLine[1] + "', '"  + nextLine[2]+ "', '" + nextLine[3] +"' )";
于 2013-09-06T16:05:44.010 に答える
1

私の場合、

残念ながら、テーブル名が一致しません。:^) お休みなさい。続きます!

于 2014-05-09T05:16:47.853 に答える