Domanda

I am creating a database in Android, all was going nice, but when I was testing the queries retrieving the correct data I've got an error.

E/AndroidRuntime(14126): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

I know that means that there is no data matching the query, but the fact is that I inserted the data by query and it actually has a match. And the same query works with all the codes that doesn't have accents.

These are the queries for inserting the rows.

INSERT INTO "codigo" VALUES('A','PEATÓN');
INSERT INTO "codigo" VALUES('B','PEATÓN');
INSERT INTO "codigo" VALUES('C','PEATÓN');

So I did a query that gets the values of the field that I was replacing, like this:

String selectCode = "select distinct c.tipo from codigo c";
Cursor cursor = database.rawQuery(selectCodigo, new String[] {});
      cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
          String codigo= cursor.getString(0);
          codigos.add(codigo);        
          System.out.println(codigo);
          cursor.moveToNext();
        } 
        cursor.close();
      return codigos;

And the result was this:

10-14 16:40:32.140: I/System.out(13716): PEAT�N

I have the text file in the /raw folder and I edited from the Eclipse so I make sure it wasn't the table export I did, but I have the same results.

This is the code that reads the file:

 public int insertFromFile(SQLiteDatabase db,Context context, int resourceId) throws IOException {
            // Reseting Counter
            int result = 0;
            // Open the resource
            InputStream insertsStream = context.getResources().openRawResource(resourceId);
            BufferedReader insertReader = new BufferedReader(new InputStreamReader(insertsStream));

            // Iterate through lines (assuming each insert has its own line and theres no other stuff)
            while (insertReader.ready()) {
                String insertStmt = insertReader.readLine();
                db.execSQL(insertStmt);
                result++;
            }
            insertReader.close();

            // returning number of inserted rows
            return result;
        } 

How could I get that accent working? I am using a lot of them, so, replacing the word is not a way out. Please help, is the only thing I need to finish this project.

UPDATE: Had the same problems two more times later. The first I fixed it by working with a .sql file coded in cp1525 and opened it in Eclipse with the default editor and find/replaced the wrong characters.

The last time I've got this error I made it the right way, and found that if you are working with SQLiteManager it imports files encoded in UTF-8 but it export files in ANSI, so I opened the file with Notepad++, change the enconding of the .sql file from ANSI to UTF-8 and it works fine, all the characters were shown fine.

È stato utile?

Soluzione

The InputStreamReader constructor documentation says:

Constructs a new InputStreamReader on the InputStream in. This constructor sets the character converter to the encoding specified in the "file.encoding" property and falls back to ISO 8859_1 (ISO-Latin-1) if the property doesn't exist.

If the file is encoded in UTF-8, you have to tell the reader this.

Altri suggerimenti

in your SQLQueryString just add before

PRAGMA encoding = "UTF-8";

example

"PRAGMA encoding = \"UTF-8\"; INSERT INTO MYTABLE (mytablefield) VALUES ('value'); "
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top