我一直在努力将CSV列表导入SQLite。我是Android开发的新手,所以我一直在这里和互联网上使用大量资源来构建我的一些代码的基础。
为了导入文件,我遵循了一种格式,所有内容似乎都是根据我的知识正确形成的,但我只是继续导入错误,我无法弄清楚发生了什么。
WellProDBHelper.java
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE BASE ("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "PUMPER_NO TEXT, "
+ "WELL_ID TEXT, "
+ "WELL_NAME TEXT, "
+ "PROD_METH TEXT, "
+ "TANK1_ID TEXT, "
+ "TANK1_COEF REAL, "
+ "TANK2_ID TEXT, "
+ "TANK2_COEF REAL, "
+ "TANK3_ID TEXT, "
+ "TANK3_COEF REAL, "
+ "CL_MTR_RUN REAL, "
+ "CL_MTR_PLATE REAL, "
+ "PLATE_FACTOR REAL, "
+ "MTR_TYPE TEXT, "
+ "MCFD_EST REAL, "
+ "ON_HRS INTEGER, "
+ "ON_MIN INTEGER, "
+ "OFF_HRS INTEGER, "
+ "OFF_MIN INTEGER, "
+ "BASE_XTRA1 TEXT, "
+ "LAT REAL, "
+ "LON REAL);");
DBImport.java
try {
InputStream is = getAssets().open("base.txt");
BufferedReader buffer = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = "";
String tableName ="BASE";
String columns = "PUMPER_NO, WELL_ID, WELL_NAME, PROD_METH, TANK1_ID, TANK1_COEF, TANK2_ID, TANK2_COEF, TANK3_ID, TANK3_COEF, CL_MTR_RUN, CL_MTR_PLATE, PLATE_FACTOR, MTR_TYPE, MCFD_EST, ON_HRS, ON_MIN, OFF_HRS, OFF_MIN, BASE_XTRA1, LAT, LON";
String str1 = "INSERT INTO " + tableName + " (" + columns + ") values(";
String str2 = ");";
SQLiteOpenHelper wellProDbHelper = new WellProDbHelper(this);
db = wellProDbHelper.getWritableDatabase();
db.beginTransaction();
while ((line = buffer.readLine()) != null) {
StringBuilder sb = new StringBuilder(str1);
String[] str = line.split(",");
//sb.append("'" + str[0] + "',"); // _id
sb.append("'" + str[0] + "',"); // PUMPER_NO
sb.append("'" + str[1] + "',"); // WELL_ID !!!! THIS IS LINE 47 WHERE THE EXCEPTION IS BEING THROWN
sb.append("'" + str[2] + "',"); // WELL_NAME
sb.append("'" + str[3] + "',"); // PROD_METH
sb.append("'" + str[4] + "',"); // TANK1_ID
sb.append(str[5] + ","); // TANK1_COEF
sb.append("'" + str[6] + "',"); // TANK2_ID
sb.append(str[7] + ","); // TANK2_COEF
sb.append("'" + str[8] + "',"); // TANK3_ID
sb.append(str[9] + ","); // TANK3_COEF
sb.append(str[10] + ","); // CL_MTR_RUN
sb.append(str[11] + ","); // CL_MTR_PLATE
sb.append(str[12] + ","); // PLATE_FACTOR
sb.append("'" + str[13] + "',"); // MTR_TYPE
sb.append(str[14] + ","); // MCFD_EST
sb.append(str[15] + ","); // ON_HRS
sb.append(str[16] + ","); // ON_MIN
sb.append(str[17] + "," ); // OFF_HRS
sb.append(str[18] + ","); // OFF_MIN
sb.append("'" + str[19] + "',"); // BASE_XTRA1
sb.append(str[20] + ","); // LAT
sb.append(str[21]); // LON
sb.append(str2);
db.execSQL(sb.toString());
}
db.setTransactionSuccessful();
db.endTransaction();
Toast toast = Toast.makeText(this, "Data Imported", Toast.LENGTH_SHORT);
toast.show();
} catch (IOException e) {
e.printStackTrace();
Toast toast = Toast.makeText(this, "File Unavailable", Toast.LENGTH_SHORT);
toast.show();
}
}
base.txt
2270,Basham #3,Basham #3,RPT,1,1.16,0,0,0,0,2,0.75,3.54,O,0,0,10,23,50,0,41.078922,-81.785875
2270,BERKEY #4,BERKEY #4- 1,RBT,07860,0.83,0,0,0,0,2,0.75,3.54,O,0,0,20,11,40,0,40.902817,-81.802522
2270,BESANCON #1,BESANCON #1,PJG,08251,0.83,0,0,0,0,2,0.125,0.095,O,0,24,0,0,0,0,40.900833,-81.783528
这是logcat
03-02 12:37:53.339 2552-2552/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hfad.wellpro, PID: 2552
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hfad.wellpro/com.hfad.wellpro.DBImport}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.hfad.wellpro.DBImport.onCreate(DBImport.java:47)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
感谢您的帮助。我一直在尝试解决这个问题,但我所做的每一项改变都会引发一个不同的异常或问题。不确定我的问题在哪里。