2017-12-28 26 views
-1

私はこの問題を修正するのに長い時間がありましたが、解決できませんでした。 私は間違ったコードはなかったと確信していましたが、エミュレータを実行すると問題はリストビュー上の特定の項目をクリックすると表示されます。私はデータベースを作成し、csvファイルからデータを入れました。ここでのコードは次のとおりE/SQLiteLog:(1)そのような列はありません:100

word_db.csv

word_db.csv

DB_Word.java

public class DB_Word extends SQLiteOpenHelper { 

public DB_Word(Context context){ 
    super(context, "word.db", null, 1); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("create table if not exists word (word_id integer primary key, category_id_fk integer, word_kor text, word_eng text, word_pro text, foreign key(category_id_fk) references category(category_id));"); 
    System.out.println("Create table word success"); 
} 

@Override 

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("drop table word;"); 
    onCreate(db); 
} 

}

Categories_wordview1.java

public class Categories_wordview1 extends AppCompatActivity { 

ArrayAdapter<String> Adapter; 
ListView wordlist; 

DB_Word dbc; 
SQLiteDatabase db; 

String sql; 
Cursor cursor; 
String[] result; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_category_wv); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    wordlist = (ListView) findViewById(R.id.wordlist); 
    dbc = new DB_Word(this); 

    delete(); 
    insertAll(); 
    select(); 
} 

public void delete(){ 
    try { 
     System.out.println("delete complete"); 
     db = dbc.getWritableDatabase(); 
     db.execSQL("delete from word;"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public void select(){ 
    System.out.println("select"); 
    try { 
     System.out.println("select try"); 
     db = dbc.getReadableDatabase(); 
     sql = "select word_kor, word_eng, word_pro from word;"; 
     cursor = db.rawQuery(sql, null); 
     int count = cursor.getCount(); 
     result = new String[count]; 

     for(int i = 0; i < count; i++){ 
      System.out.println("select for"); 
      cursor.moveToNext(); 
      String str_kor = cursor.getString(0); 
      String str_eng = cursor.getString(1); 
      String str_pro = cursor.getString(2); 
      result[i] = str_kor + " " + str_eng + " " + str_pro; 
     } 

     Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, result); 
     wordlist.setAdapter(Adapter); 
     wordlist.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public void insertAll(){ 
    System.out.println("insert"); 

    db = dbc.getWritableDatabase(); 
    InputStream is = this.getResources().openRawResource(R.raw.word_db); 
    BufferedReader buffer = new BufferedReader(new InputStreamReader(is)); 
    String tableName ="word"; 
    String columns = "word_id, category_id_fk, word_kor"; 
    String str1 = "insert into " + tableName + "(" + columns + ") values("; 
    String str2 = ");"; 
    String line =""; 

    db.beginTransaction(); 
    try { 
     System.out.println("transaction try"); 
     while ((line = buffer.readLine()) != null) { 
      System.out.println("transaction while"); 
      StringBuilder sb = new StringBuilder(str1); 
      String[] str = line.split(","); 
      sb.append(str[0] + ", "); 
      sb.append(str[1] + ", '"); 
      sb.append(str[2] + "'"); 
      sb.append(str2); 
      System.out.println(sb.toString()); 
      db.execSQL(sb.toString()); 
      //db.rawQuery(sb.toString(), null); 
     } 
     db.setTransactionSuccessful(); 
     db.endTransaction(); 
    } 
    catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
    finally { 
      try { 
       is.close(); 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
      } 
    } 

} 

}

ここでlogcatを示している:

12-28 00:04:05.106 1554-1651/? E/TaskPersister: File error accessing recents directory (directory doesn't exist?). 
12-28 00:04:15.182 1554-1795/? I/ActivityManager: START u0 {cmp=com.example.project2/.Categories_word.Categories_wordview1} from uid 10009 
12-28 00:04:15.194 1380-2231/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 3729276 , only wrote 3728880 
12-28 00:04:15.315 3259-3259/? I/System.out: delete complete 
12-28 00:04:15.401 3259-3259/? I/System.out: Create table word success 
12-28 00:04:15.409 3259-3259/? I/System.out: insert 
12-28 00:04:15.411 3259-3259/? I/System.out: transaction try 
12-28 00:04:15.412 3259-3259/? I/System.out: transaction while 
12-28 00:04:15.412 3259-3259/? I/System.out: insert into word(word_id, category_id_fk, word_kor) values(100, 100, '안녕하세요'); 
12-28 00:04:15.413 3259-3259/? E/SQLiteLog: (1) no such column: 100 
12-28 00:04:15.413 3259-3259/? D/AndroidRuntime: Shutting down VM 
12-28 00:04:15.418 3259-3259/? E/AndroidRuntime: FATAL EXCEPTION: main 
               Process: com.example.project2, PID: 3259 
               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project2/com.example.project2.Categories_word.Categories_wordview1}: android.database.sqlite.SQLiteException: no such column: 100 (code 1): , while compiling: insert into word(word_id, category_id_fk, word_kor) values(100, 100, '안녕하세요'); 
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                at android.os.Handler.dispatchMessage(Handler.java:105) 
                at android.os.Looper.loop(Looper.java:164) 
                at android.app.ActivityThread.main(ActivityThread.java:6541) 
                at java.lang.reflect.Method.invoke(Native Method) 
                at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
                Caused by: android.database.sqlite.SQLiteException: no such column: 100 (code 1): , while compiling: insert into word(word_id, category_id_fk, word_kor) values(100, 100, '안녕하세요'); 
                at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
                at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
                at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
                at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
                at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
                at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
                at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677) 
                at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608) 
                at com.example.project2.Categories_word.Categories_wordview1.insertAll(Categories_wordview1.java:117) 
                at com.example.project2.Categories_word.Categories_wordview1.onCreate(Categories_wordview1.java:51) 
                at android.app.Activity.performCreate(Activity.java:6975) 
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) 
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  
                at android.app.ActivityThread.-wrap11(Unknown Source:0)  
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  
                at android.os.Handler.dispatchMessage(Handler.java:105)  
                at android.os.Looper.loop(Looper.java:164)  
                at android.app.ActivityThread.main(ActivityThread.java:6541)  
                at java.lang.reflect.Method.invoke(Native Method)  
                at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  
12-28 00:04:15.483 1554-1566/? W/ActivityManager: Force finishing activity com.example.project2/.Categories_word.Categories_wordview1 
12-28 00:04:15.503 1554-1566/? W/ActivityManager: Force finishing activity com.example.project2/.Categories 

これは問題です:

E/SQLiteLog:(1)そのような列はありません:100

android.database.sqlite.SQLiteException:そのような列はありません:100(コード1):、コンパイル中:単語に挿入(word_id、category_id_fk、word_kor)値(100,100、 '안녕하세요');

答えて

0

CSVファイルの最初の行には、ファイルをUnicodeを含むものとしてマークするために、一部のWindowsプログラムによって書き込まれる非表示の制御文字(ゼロ幅の区切りなしの空白)が含まれています。それを除く。

関連する問題