E / SQLiteLog:(1)没有这样的列:100

时间:2017-12-28 17:45:33

标签: java android sqlite csv

我有很长时间来解决这个问题,但无法解决。 我确信没有错误的代码,但如果我执行模拟器,则问题是当我单击列表视图上的特定项目时,它会显示,然后再次打开应用程序"。我创建数据库并从csv文件中放入数据。以下是代码:

  

word_db.csv

word_db.csv

  

DB_Word.java

$sets = [];

if ($genres){
    $sets['genres'] = $genres;
}

if ($themes){
    $sets['themes'] = $themes;
}

$arr = array();
foreach( $sets as $type => $data ){
    foreach ($genres as $i) {
            $arr[$type][] = $i->name;
    }
}

}

  

Categories_wordview1.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);
}

}

  

这是logcat显示:

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

}

这就是问题所在:

E / SQLiteLog:(1)没有这样的列:100

android.database.sqlite.SQLiteException:没有这样的列:100(代码1):,同时编译:插入word(word_id,category_id_fk,word_kor)值(100,100,&#39;안녕하세요&# 39);

1 个答案:

答案 0 :(得分:0)

CSV文件的第一行包含一个不可见的控制字符(零宽度不间断空格),由某些Windows程序写入以将文件标记为包含Unicode。删除它。