我在编写一个示例程序时遇到了一些麻烦。
这是我的代码:
public void prePopulateDatabase(SQLiteDatabase db, Context helperContext){
String fileIn = "";
String[] file;
ContentValues row = new ContentValues();
AssetManager am;
InputStream is = null;
am = helperContext.getResources().getAssets();
try {
is = am.open("Matrix.csv");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while((fileIn = br.readLine()) != null){
file = fileIn.split(",");
row.put(KEYWORD, file[0]);
row.put(DURATION, file[1]);
row.put(AMOUNT, file[2]);
row.put(DESCRIPTION, file[3]);
row.put(LEVEL_NAME, file[4]);
row.put(CHOICE_FOR_TWO_1, file[5]);
row.put(CHOICE_FOR_TWO_2, file[6]);
row.put(CHOICE_FOR_FIVE_1, file[7]);
row.put(CHOICE_FOR_FIVE_2, file[8]);
row.put(CHOICE_FOR_FIVE_3, file[9]);
row.put(CHOICE_FOR_FIVE_4, file[10]);
row.put(CHOICE_FOR_FIVE_5, file[11]);
insertToDBViaContentValues(db, row);
}//end while
} catch (Exception e) {
e.printStackTrace();
}//end try catch
}//end fxn
public void insertToDBViaContentValues(SQLiteDatabase db, ContentValues row){
procedure("SiteMatrixMultiLevels: INSERT TO DB > "+row.toString());
db.insert(TABLE_SITES, null, row);
}
public void validate(SQLiteDatabase db){
Cursor c = db.rawQuery("select * from "+TABLE_SITES, null);
if(c != null && c.moveToFirst()){
while(!c.isAfterLast()){
data("VALUE: " + c.getString(0) + "," + c.getString(1) + "," + c.getString(2) + "," + c.getString(3) + "," + c.getString(4) + "," + c.getString(5)
+ "," + c.getString(6) + "," + c.getString(7) + "," + c.getString(8) + "," + c.getString(9) + "," + c.getString(10)
+ "," + c.getString(11) + "," + c.getString(12));
c.moveToNext();
}
}
}
我收到错误
02-03 20:24:04.537: W/System.err(16437): java.lang.ArrayIndexOutOfBoundsException: length=7; index=7
在
row.put(CHOICE_FOR_FIVE_1, file[7]);
我从中获取文件的csv文件有多行,但只有2种格式。 前100个是这种格式
pkey,keyword,duration,amount,description,A,B,,,,,
而另外100人遵循这种格式
pkey,keyword,duration,amount,description,,,C,D,E,F,G
我不确定我是否收到错误,因为第一种格式类型仅在7处停止(因为没有指定C,D,E,F,G的值。)
任何帮助将不胜感激。谢谢!
答案 0 :(得分:1)
您的数据没有问题。以下是split()
电话中的所需内容......
file = fileIn.split(",", -1);
多数人......
实际上,您需要查阅split(String regex)的文档,正如它所说的那样,
此方法的工作方式就像通过调用双参数split方法一样 给定的表达式和一个零的限制参数。尾随空 因此,字符串不包含在结果数组中。
表示您需要在其中定义第二个参数(limit),否则它会将其视为zero '0'
,并且将删除空条目。所以传递-1以获取所有空条目。
希望这会有所帮助......
答案 1 :(得分:0)
简单的实施。尝试使用StringTokenizer。
private void readAndInsert() throws UnsupportedEncodingException {
ArrayList<SimpleQuestion> questions = new ArrayList<SimpleQuestion>();
AssetManager assetManager = getAssets();
InputStream is = null;
try {
is = assetManager.open("questions/question_bank.csv");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String line = "";
StringTokenizer st = null;
try {
while ((line = reader.readLine()) != null) {
st = new StringTokenizer(line, ",");
SimpleQuestion sQuestion= new SimpleQuestion ();
//your attributes
sQuestion.setX(st.nextToken());
sQuestion.setY(st.nextToken());
sQuestion.setZ(st.nextToken());
sQuestion.setW(st.nextToken());
questions .add(sQuestion);
}
} catch (IOException e) {
e.printStackTrace();
}
yourDB.bulkInsert(questions );
}