执行代码时出现此错误。
无法从CursorWindow读取第0行,col -1。在从中访问数据之前,请确保Cursor已正确初始化。
我的Db代码如下
package com.routecounselor;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class Project_DB extends SQLiteOpenHelper {
public Project_DB(Context context) {
super(context, "mydb.db", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE route(routeId INTEGER PRIMARY KEY AUTOINCREMENT,routeNumber VARCHAR(5))");
db.execSQL("CREATE TABLE stop(stopId INTEGER PRIMARY KEY AUTOINCREMENT,stopName VARCHAR(40),lat double,lon double)");
db.execSQL("CREATE TABLE relation(relationId INTEGER PRIMARY KEY AUTOINCREMENT,routeId INTEGER, stopId INTEGER,"
+ "FOREIGN KEY(routeId) REFERENCES route(routeId), FOREIGN KEY(stopId) REFERENCES stop(stopId))");
Log.d("LOGCAT", "Route Table Created");
Log.d("LOGCAT", "Stop Table Created");
Log.d("LOGCAT", "Relation Table Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void insertroute() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("routeNumber", "101");
db.insert("route", null, values);
values.put("routeNumber", "104");
db.insert("route", null, values);
values.put("routeNumber", "104-A");
db.insert("route", null, values);
values.put("routeNumber", "105");
db.insert("route", null, values);
values.put("routeNumber", "105-A");
db.insert("route", null, values);
values.put("routeNumber", "110");
db.insert("route", null, values);
values.put("routeNumber", "111");
db.insert("route", null, values);
values.put("routeNumber", "113-A");
db.insert("route", null, values);
values.put("routeNumber", "115");
db.insert("route", null, values);
values.put("routeNumber", "120");
db.insert("route", null, values);
values.put("routeNumber", "121");
db.insert("route", null, values);
values.put("routeNumber", "122");
db.insert("route", null, values);
values.put("routeNumber", "122-A");
db.insert("route", null, values);
values.put("routeNumber", "124");
db.insert("route", null, values);
values.put("routeNumber", "124-A");
db.insert("route", null, values);
values.put("routeNumber", "127");
db.insert("route", null, values);
values.put("routeNumber", "127-A");
db.insert("route", null, values);
values.put("routeNumber", "128");
db.insert("route", null, values);
values.put("routeNumber", "131");
db.insert("route", null, values);
values.put("routeNumber", "133");
db.insert("route", null, values);
values.put("routeNumber", "136");
db.insert("route", null, values);
values.put("routeNumber", "138");
db.insert("route", null, values);
values.put("routeNumber", "1");
db.insert("route", null, values);
values.put("routeNumber", "1-C");
db.insert("route", null, values);
values.put("routeNumber", "3");
db.insert("route", null, values);
values.put("routeNumber", "6");
db.insert("route", null, values);
values.put("routeNumber", "7");
db.insert("route", null, values);
values.put("routeNumber", "21");
db.insert("route", null, values);
values.put("routeNumber", "24");
db.insert("route", null, values);
db.close();
}
public void insertstopName() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values1 = new ContentValues();
values1.put("stopName", "Mandimor");
values1.put("lat", "33.637358");
values1.put("lon", "73.035063");
db.insert("stop", null, values1);
values1.put("stopName", "Pindora");
values1.put("lat", "33.652172");
values1.put("lon", "73.064414");
db.insert("stop", null, values1);
values1.put("stopName", "Faizabad");
values1.put("lat", "33.663157");
values1.put("lon", " 73.085276");
db.insert("stop", null, values1);
values1.put("stopName", "Zeropoint");
values1.put("lat", "33.678763");
values1.put("lon", "73.076445");
db.insert("stop", null, values1);
values1.put("stopName", "Aabpara");
values1.put("lat", " 33.707109");
values1.put("lon", " 73.089308");
db.insert("stop", null, values1);
values1.put("stopName", "Super Market");
values1.put("lat", "33.729444");
values1.put("lon", "73.077584");
db.insert("stop", null, values1);
values1.put("stopName", "Tarnol");
values1.put("lat", " 33.649919");
values1.put("lon", " 72.910876");
db.insert("stop", null, values1);
values1.put("stopName", "G-10/1");
values1.put("lat", " 33.670376");
values1.put("lon", " 73.010287");
db.insert("stop", null, values1);
values1.put("stopName", "G-10/2");
values1.put("lat", " 33.677019");
values1.put("lon", " 73.005395");
db.insert("stop", null, values1);
values1.put("stopName", "G-10/3");
values1.put("lat", " 33.686465");
values1.put("lon", "73.020801");
db.insert("stop", null, values1);
values1.put("stopName", "G-9/3");
values1.put("lat", " 33.696266");
values1.put("lon", " 73.033483");
db.insert("stop", null, values1);
values1.put("stopName", "Pims");
values1.put("lat", " 33.707050");
values1.put("lon", " 73.054187");
db.insert("stop", null, values1);
values1.put("stopName", "G-7 Markaz");
values1.put("lat", " 33.720329");
values1.put("lon", " 73.058162");
db.insert("stop", null, values1);
values1.put("stopName", "Lal Quarter");
values1.put("lat", " 33.717243");
values1.put("lon", " 73.073263");
db.insert("stop", null, values1);
values1.put("stopName", "Polyclinic");
values1.put("lat", " 33.720902");
values1.put("lon", " 73.080108");
db.insert("stop", null, values1);
values1.put("stopName", "Pak Secretariat");
values1.put("lat", " 33.737451");
values1.put("lon", " 73.091737");
db.insert("stop", null, values1);
values1.put("stopName", "Islamabad Chok");
values1.put("lat", " 33.632182");
values1.put("lon", " 72.938578");
db.insert("stop", null, values1);
values1.put("stopName", "G-11/1");
values1.put("lat", " 33.658188");
values1.put("lon", " 72.995135");
db.insert("stop", null, values1);
values1.put("stopName", "G-11/4");
values1.put("lat", " 33.664504");
values1.put("lon", " 73.000978");
db.insert("stop", null, values1);
values1.put("stopName", "G-10 Markaz");
values1.put("lat", " 33.676794");
values1.put("lon", " 73.014791");
db.insert("stop", null, values1);
values1.put("stopName", "G-9 Markaz");
values1.put("lat", "33.689885");
values1.put("lon", "73.030500");
db.insert("stop", null, values1);
values1.put("stopName", "Blue Area");
values1.put("lat", " 33.710953");
values1.put("lon", " 73.060684");
db.insert("stop", null, values1);
values1.put("stopName", "Parliament Chok ");
values1.put("lat", "33.729316 ");
values1.put("lon", "73.093008");
db.insert("stop", null, values1);
values1.put("stopName", "G-15");
values1.put("lat", "33.633175");
values1.put("lon", "72.922142");
db.insert("stop", null, values1);
values1.put("stopName", "T and T Chok");
values1.put("lat", "33.694879");
values1.put("lon", "73.064160");
db.insert("stop", null, values1);
values1.put("stopName", "G-7/2");
values1.put("lat", "33.702493");
values1.put("lon", "73.066129");
db.insert("stop", null, values1);
values1.put("stopName", "Meloody");
values1.put("lat", "33.716464");
values1.put("lon", "73.083493");
db.insert("stop", null, values1);
values1.put("stopName", "Foreign Office");
values1.put("lat", "33.718699");
values1.put("lon", "73.103647");
db.insert("stop", null, values1);
values1.put("stopName", "Chattar");
values1.put("lat", "33.779976");
values1.put("lon", "73.240520");
db.insert("stop", null, values1);
values1.put("stopName", "Bara Kau");
values1.put("lat", "33.739015");
values1.put("lon", "73.178387");
db.insert("stop", null, values1);
values1.put("stopName", "Malpur");
values1.put("lat", "33.730146");
values1.put("lon", "73.145852");
db.insert("stop", null, values1);
values1.put("stopName", "RawalDam Chok");
values1.put("lat", "33.708651");
values1.put("lon", "73.106199");
db.insert("stop", null, values1);
db.close();
}
public void insertrelation() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values3 = new ContentValues();
values3.put("routeId", "1");
values3.put("stopId", "1");
db.insert("relation", null, values3);
values3.put("routeId", "1");
values3.put("stopId", "2");
db.insert("relation", null, values3);
values3.put("routeId", "1");
values3.put("stopId", "3");
db.insert("relation", null, values3);
values3.put("routeId", "1");
values3.put("stopId", "4");
db.insert("relation", null, values3);
values3.put("routeId", "1");
values3.put("stopId", "5");
db.insert("relation", null, values3);
values3.put("routeId", "1");
values3.put("stopId", "6");
db.insert("relation", null, values3);
values3.put("routeId", "1");
values3.put("stopId", "49");
db.insert("relation", null, values3);
values3.put("routeId", "2");
values3.put("stopId", "7");
db.insert("relation", null, values3);
values3.put("routeId", "2");
values3.put("stopId", "8");
db.insert("relation", null, values3);
values3.put("routeId", "2");
values3.put("stopId", "9");
db.insert("relation", null, values3);
values3.put("routeId", "2");
values3.put("stopId", "10");
db.insert("relation", null, values3);
values3.put("routeId", "2");
values3.put("stopId", "11");
db.insert("relation", null, values3);
values3.put("routeId", "2");
values3.put("stopId", "12");
db.insert("relation", null, values3);
values3.put("routeId", "2");
values3.put("stopId", "13");
db.insert("relation", null, values3);
values3.put("routeId", "2");
values3.put("stopId", "14");
db.insert("relation", null, values3);
values3.put("routeId", "2");
values3.put("stopId", "15");
db.insert("relation", null, values3);
values3.put("routeId", "2");
values3.put("stopId", "6");
db.insert("relation", null, values3);
values3.put("routeId", "2");
values3.put("stopId", "16");
db.insert("relation", null, values3);
values3.put("routeId", "3");
values3.put("stopId", "7");
db.insert("relation", null, values3);
values3.put("routeId", "3");
values3.put("stopId", "17");
db.insert("relation", null, values3);
values3.put("routeId", "3");
values3.put("stopId", "18");
db.insert("relation", null, values3);
values3.put("routeId", "3");
values3.put("stopId", "19");
db.insert("relation", null, values3);
values3.put("routeId", "3");
values3.put("stopId", "20");
db.insert("relation", null, values3);
values3.put("routeId", "3");
values3.put("stopId", "21");
db.insert("relation", null, values3);
values3.put("routeId", "3");
values3.put("stopId", "12");
db.insert("relation", null, values3);
values3.put("routeId", "3");
values3.put("stopId", "22");
db.insert("relation", null, values3);
values3.put("routeId", "3");
values3.put("stopId", "15");
db.insert("relation", null, values3);
values3.put("routeId", "3");
values3.put("stopId", "23");
db.insert("relation", null, values3);
values3.put("routeId", "3");
values3.put("stopId", "16");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "24");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "19");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "20");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "9");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "10");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "21");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "25");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "26");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "13");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "14");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "15");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "27");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "5");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "28");
db.insert("relation", null, values3);
values3.put("routeId", "4");
values3.put("stopId", "16");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "24");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "20");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "21");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "25");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "26");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "13");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "14");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "15");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "27");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "5");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "28");
db.insert("relation", null, values3);
values3.put("routeId", "5");
values3.put("stopId", "16");
db.insert("relation", null, values3);
values3.put("routeId", "6");
values3.put("stopId", "29");
db.insert("relation", null, values3);
values3.put("routeId", "6");
values3.put("stopId", "30");
db.insert("relation", null, values3);
values3.put("routeId", "6");
values3.put("stopId", "31");
db.insert("relation", null, values3);
values3.put("routeId", "6");
values3.put("stopId", "32");
db.insert("relation", null, values3);
values3.put("routeId", "6");
values3.put("stopId", "3");
db.insert("relation", null, values3);
values3.put("routeId", "6");
values3.put("stopId", "2");
db.insert("relation", null, values3);
values3.put("routeId", "6");
values3.put("stopId", "33");
db.insert("relation", null, values3);
values3.put("routeId", "6");
values3.put("stopId", "1");
db.insert("relation", null, values3);
values3.put("routeId", "7");
values3.put("stopId", "34");
db.insert("relation", null, values3);
values3.put("routeId", "7");
values3.put("stopId", "35");
db.insert("relation", null, values3);
values3.put("routeId", "7");
values3.put("stopId", "36");
db.insert("relation", null, values3);
values3.put("routeId", "7");
values3.put("stopId", "3");
db.insert("relation", null, values3);
values3.put("routeId", "7");
values3.put("stopId", "37");
db.insert("relation", null, values3);
values3.put("routeId", "7");
values3.put("stopId", "38");
db.insert("relation", null, values3);
values3.put("routeId", "7");
values3.put("stopId", "12");
db.insert("relation", null, values3);
values3.put("routeId", "7");
values3.put("stopId", "39");
db.insert("relation", null, values3);
values3.put("routeId", "8");
values3.put("stopId", "1");
db.insert("relation", null, values3);
values3.put("routeId", "8");
values3.put("stopId", "40");
db.insert("relation", null, values3);
values3.put("routeId", "8");
values3.put("stopId", "41");
db.insert("relation", null, values3);
values3.put("routeId", "8");
values3.put("stopId", "21");
db.insert("relation", null, values3);
values3.put("routeId", "8");
values3.put("stopId", "42");
db.insert("relation", null, values3);
values3.put("routeId", "8");
values3.put("stopId", "12");
db.insert("relation", null, values3);
values3.put("routeId", "8");
values3.put("stopId", "22");
db.insert("relation", null, values3);
values3.put("routeId", "8");
values3.put("stopId", "6");
db.insert("relation", null, values3);
values3.put("routeId", "8");
values3.put("stopId", "16");
db.insert("relation", null, values3);
values3.put("routeId", "9");
values3.put("stopId", "1");
db.insert("relation", null, values3);
values3.put("routeId", "9");
values3.put("stopId", "48");
db.insert("relation", null, values3);
values3.put("routeId", "9");
values3.put("stopId", "40");
db.insert("relation", null, values3);
values3.put("routeId", "9");
values3.put("stopId", "41");
db.insert("relation", null, values3);
values3.put("routeId", "9");
values3.put("stopId", "21");
db.insert("relation", null, values3);
values3.put("routeId", "9");
values3.put("stopId", "20");
db.insert("relation", null, values3);
values3.put("routeId", "9");
values3.put("stopId", "43");
db.insert("relation", null, values3);
values3.put("routeId", "9");
values3.put("stopId", "44");
db.insert("relation", null, values3);
values3.put("routeId", "9");
values3.put("stopId", "18");
db.insert("relation", null, values3);
db.close();
}
public String matchStop(String data1,String data2)
{
String ab=null;
String Source=null;
String Destination=null;
String latitude1=null;
String longitude1=null;
String latitude2=null;
String longitude2=null;
SQLiteDatabase db=this.getReadableDatabase();
String select1Query = "SELECT * FROM stop WHERE stopName='"+data1+"'";
Cursor cursor=db.rawQuery(select1Query,null);
if(cursor.moveToFirst()){
do{
Source=cursor.getString(cursor.getColumnIndex("stopName"));
latitude1=cursor.getString(cursor.getColumnIndex("lat"));
longitude1=cursor.getString(cursor.getColumnIndex("lon"));
/*Log.e("LAt Lan",latitude+longitude+"");
Log.e("N",Source+"");*/
}
while(cursor.moveToNext());
}
String select2Query = "SELECT * FROM stop WHERE stopName='"+data2+"'";
Cursor cursor1=db.rawQuery(select2Query,null);
if(cursor1.moveToFirst()){
do{
Destination=cursor1.getString(cursor1.getColumnIndex("stopName"));
latitude2=cursor1.getString(cursor1.getColumnIndex("lat"));
longitude2=cursor1.getString(cursor1.getColumnIndex("lon"));
}
while(cursor.moveToNext());
if(Source!=null && Destination!=null)
{
ab= (Source+",,"+Destination+",,"+latitude1+",,"+longitude1+",,"+latitude2+",,"+longitude2);
}
else
{
}
if(latitude1!=null && longitude1!=null){
}
}
return ab;
}
public String getroutenumber(String Source,String Destination) {
System.out.println("Inside GetRouteNumber");
String number=null;
String number1 = null;
String number2 = null;
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery1="SELECT * FROM stop WHERE stopName='"+Source+"'";
Cursor cursor1 = db.rawQuery(selectQuery1, null);
if (cursor1.moveToFirst()) {
do {
number1 = cursor1.getString(cursor1.getColumnIndex("stopId"));
Log.e("Source id", number1 );
}
while (cursor1.moveToNext());
}
String selectQuery2="SELECT * FROM stop WHERE stopName='"+Destination+"'";
Cursor cursor2 = db.rawQuery(selectQuery2, null);
if (cursor2.moveToFirst()) {
do {
number2 = cursor2.getString(cursor2.getColumnIndex("stopId"));
Log.e("Destination Id", number2);
}
while (cursor1.moveToNext());
}
String selectQuery = "SELECT stopId FROM relation WHERE routeId in(SELECT routeId FROM relation WHERE stopId='"+number1+"')and stopId in(SELECT stopId FROM relation WHERE routeId in(SELECT routeId FROM relation WHERE stopId='"+number2+"'))";
Cursor cursor = db.rawQuery(selectQuery, null);
System.out.println(cursor);
if (cursor.moveToFirst()) {
do {
Log.e("inside", "of do");
number = cursor.getString(cursor.getColumnIndex("routeNumber"));
Log.e("Route Number", number );
}
while (cursor.moveToNext());
}
return number;
}
}
我的猫咪日志
08-18 00:06:31.957: E/AndroidRuntime(22127): FATAL EXCEPTION: main
08-18 00:06:31.957: E/AndroidRuntime(22127): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
08-18 00:06:31.957: E/AndroidRuntime(22127): at android.database.CursorWindow.nativeGetString(Native Method)
08-18 00:06:31.957: E/AndroidRuntime(22127): at android.database.CursorWindow.getString(CursorWindow.java:434)
08-18 00:06:31.957: E/AndroidRuntime(22127): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
08-18 00:06:31.957: E/AndroidRuntime(22127): at com.routecounselor.Project_DB.getroutenumber(Project_DB.java:2141)
08-18 00:06:31.957: E/AndroidRuntime(22127): at com.routecounselor.Map$1.onClick(Map.java:136)
08-18 00:06:31.957: E/AndroidRuntime(22127): at android.view.View.performClick(View.java:4421)
08-18 00:06:31.957: E/AndroidRuntime(22127): at android.view.View$PerformClick.run(View.java:17903)
08-18 00:06:31.957: E/AndroidRuntime(22127): at android.os.Handler.handleCallback(Handler.java:730)
08-18 00:06:31.957: E/AndroidRuntime(22127): at android.os.Handler.dispatchMessage(Handler.java:92)
08-18 00:06:31.957: E/AndroidRuntime(22127): at android.os.Looper.loop(Looper.java:213)
08-18 00:06:31.957: E/AndroidRuntime(22127): at android.app.ActivityThread.main(ActivityThread.java:5225)
08-18 00:06:31.957: E/AndroidRuntime(22127): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 00:06:31.957: E/AndroidRuntime(22127): at java.lang.reflect.Method.invoke(Method.java:525)
08-18 00:06:31.957: E/AndroidRuntime(22127): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
08-18 00:06:31.957: E/AndroidRuntime(22127): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-18 00:06:31.957: E/AndroidRuntime(22127): at dalvik.system.NativeStart.main(Native Method)
有人可以检测错误的位置吗?
答案 0 :(得分:0)
LogCat中显示的错误表示当您从getColumnIndex()
内调用getroutenumber()
时,它返回 -1 ,表示检索到的数据中不存在列名称组。确保您的SQL查询正确并且列名与表中的列完全匹配。如果您无法解决问题,请尝试使用显式列索引逐步替换(仅用于调试)getColumnIndex()方法,并查看错误弹出的位置。