samedi 3 janvier 2015

Android/SQLite - Datatype Mismatch

I am working on an Android app and I am using SQLite to make a database, however im receiving some very strange feedback. I am getting an error when I attempt to insert which says datatype mismatch but I am pretty sure I am inserting a valid tuple.


What really confuses me is my error log. The log contains the following line:



at com.example.myshuttle.MySQLiteHelper.addAgency(MySQLiteHelper.java:80)

Here is what is strange. I made the class MySQLiteHelper and at one point it had a method called addAgency, but it no longer has this method now. I replaced the 'addAgency' method with a more generic method called 'add'. I've searched my code and at no point do I call the addAgency method. Even stranger is that line 80 of MySQLiteHelper is a blank line.


It appears that any changes to my code are not being compiled or ran. Has anyone experienced this before or does anyone have any suggestions? I will include relevant code below and a full error log below.


Here is the code from the MySQLiteHelper class. This only contains variables and method pertaining to the Agency table.



private static final String TABLE_AGENCY = "agency";
private static final String KEY_AGENCY_PHONE = "phone";
private static final String KEY_AGENCY_FARE_URL = "fare_url";
private static final String KEY_AGENCY_URL = "url";
private static final String KEY_AGENCY_ID = "id";
private static final String KEY_AGENCY_NAME = "name";
private static final String KEY_AGENCY_TIMEZONE = "timezone";
private static final String KEY_AGENCY_LANG = "lang";
private static final String[] COLUMNS_AGENCY = {KEY_AGENCY_PHONE, KEY_AGENCY_FARE_URL,
KEY_AGENCY_URL, KEY_AGENCY_ID, KEY_AGENCY_NAME, KEY_AGENCY_TIMEZONE, KEY_AGENCY_LANG};

private void loadAgency() throws IOException{
String sqlCmd;
AssetManager am = context.getAssets();
InputStream is = am.open("agency.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
SQLiteDatabase db = this.getReadableDatabase();

String line = reader.readLine();

while ((line = reader.readLine()) != null) {

String[] attributes = line.split(",");
this.add(new Agency(attributes));
}
}

public void add(Agency input){
Log.e("addAgency", input.toString());

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_AGENCY_PHONE, input.phone);
values.put(KEY_AGENCY_FARE_URL, input.fare_url);
values.put(KEY_AGENCY_URL, input.url);
values.put(KEY_AGENCY_ID, input.id);
values.put(KEY_AGENCY_NAME, input.name);
values.put(KEY_AGENCY_TIMEZONE, input.timezone);
values.put(KEY_AGENCY_LANG, input.lang);

db.insert(TABLE_AGENCY, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values

db.close();
}

public Agency getAgency(String id){

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor =
db.query(TABLE_AGENCY, // a. table
COLUMNS_AGENCY, // b. column name
" id = ?", // c. selections
new String[] { id }, // d. seletions args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit

if (cursor != null)
cursor.moveToFirst();

String[] attributes = {cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getInt(3) + "", cursor.getString(4),
cursor.getString(5), cursor.getString(6)};

Agency a = new Agency(attributes);

Log.e("getAgency("+id+")", a.toString());

return a;
}


Here is the Agency class



public class Agency {


public String phone;
public String fare_url;
public String url;
public String id;
public String name;
public String timezone;
public String lang;

public Agency(){}

public Agency(String[] attributes){
this.phone = attributes[0];
this.fare_url = attributes[1];
this.url = attributes[2];
this.id = attributes[3];
this.name = attributes[4];
this.timezone = attributes[5];
this.lang = attributes[6];
}

public String toString(){
return "Agency [phone=" + phone + ", fare_url=" + fare_url + ", url=" + url
+ ", id=" + id + ", name=" + name + ", timezone= " + timezone
+ ", lang=" + lang +"]";
}


}


Heres my full error log



12-23 16:13:44.601: E/addBook(15162): Agency [phone=905-527-4441, fare_url=null, url=http://ift.tt/1qCKG7O, id=HSR, name=Hamilton Street Railway, timezone= America/Toronto, lang=en]
12-23 16:13:44.611: E/SQLiteLog(15162): (20) statement aborts at 6: [INSERT INTO agency(id,timezone,phone,fare_url,lang,url,name) VALUES (?,?,?,?,?,?,?)] datatype mismatch
12-23 16:13:44.611: E/SQLiteDatabase(15162): Error inserting id=HSR timezone=America/Toronto phone=905-527-4441 fare_url=null lang=en url=http://ift.tt/1qCKG7O name=Hamilton Street Railway
12-23 16:13:44.611: E/SQLiteDatabase(15162): android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:972)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1603)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1473)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at com.example.myshuttle.MySQLiteHelper.addAgency(MySQLiteHelper.java:80)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at com.example.myshuttle.MainActivity.onCreate(MainActivity.java:39)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.Activity.performCreate(Activity.java:5451)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.ActivityThread.access$900(ActivityThread.java:169)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.os.Handler.dispatchMessage(Handler.java:102)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.os.Looper.loop(Looper.java:136)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.ActivityThread.main(ActivityThread.java:5479)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at java.lang.reflect.Method.invoke(Method.java:515)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at dalvik.system.NativeStart.main(Native Method)
12-23 16:13:44.981: E/Map Zoom(15162): 15.0
12-23 16:13:44.991: E/AndroidRuntime(15162): FATAL EXCEPTION: main
12-23 16:13:44.991: E/AndroidRuntime(15162): Process: com.example.myshuttle, PID: 15162
12-23 16:13:44.991: E/AndroidRuntime(15162): java.lang.NullPointerException
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.example.myshuttle.MainActivity$1.onCameraChange(MainActivity.java:135)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.google.android.gms.maps.GoogleMap$7.onCameraChange(Unknown Source)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.google.android.gms.maps.internal.e$a.onTransact(Unknown Source)
12-23 16:13:44.991: E/AndroidRuntime(15162): at android.os.Binder.transact(Binder.java:361)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.google.android.gms.maps.internal.af.a(SourceFile:93)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.google.maps.api.android.lib6.gmm6.c.c.a(Unknown Source)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.google.maps.api.android.lib6.gmm6.c.d.run(Unknown Source)
12-23 16:13:44.991: E/AndroidRuntime(15162): at android.os.Handler.handleCallback(Handler.java:733)
12-23 16:13:44.991: E/AndroidRuntime(15162): at android.os.Handler.dispatchMessage(Handler.java:95)
12-23 16:13:44.991: E/AndroidRuntime(15162): at android.os.Looper.loop(Looper.java:136)
12-23 16:13:44.991: E/AndroidRuntime(15162): at android.app.ActivityThread.main(ActivityThread.java:5479)
12-23 16:13:44.991: E/AndroidRuntime(15162): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 16:13:44.991: E/AndroidRuntime(15162): at java.lang.reflect.Method.invoke(Method.java:515)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-23 16:13:44.991: E/AndroidRuntime(15162): at dalvik.system.NativeStart.main(Native Method)

Aucun commentaire:

Enregistrer un commentaire