[android-beginners] Re: Best way to add 100.000 words to database

2009-06-10 Thread kaloer

I think you are right. I'll look at it,
Thank you very much

//Kaloer

On 2 Jun., 13:05, kaloer mkal...@gmail.com wrote:
 Btw, this is how the app works:

 There is a text view and a button. The user will enter a single word,
 and when he presses the button, the application checks whether this is
 actually a real word.

 On 2 Jun., 09:27, kaloer mkal...@gmail.com wrote:

  It is a list of english words, that shall be used as a spell checker.
  I have tried to use a txt file and put the data to a List, but this is
  too slow because it needs to read all the data. But Maybe I should
  create a table in the database for each letter (and every second, if
  necessary) , so that it doesn't need to read all the data.

  On 1 Jun., 18:28, Raphael r...@android.com wrote:

   This begs to a different kind of question: do you really need to store
   this data in an sqlite3 database?

   There are other alternatives, it all depends on your data and how you
   want to use it so you might want to help us here.

   Example: you mention words, just not any string, so your data might
   be a dictionary.
   Then in this case you could simply use a binary file, in your own
   format, packaged in your project/res/raw directory, which you can
   retrieve 
   withhttp://d.android.com/reference/android/content/res/Resources.html#ope...)
   -- the file is added uncompressed to your APK but the APK itself is a
   zip file, which is why you get an input stream. That also means if you
   need direct random access you'll probably want to write it somewhere,
   such as on the sdcard.
   Make sure to write an index for fast lookup, for example the offset of
   words starting by A, by B, etc. You will probably need to experiment a
   bit to get a tradeof between simplicity of the index and lookup time,
   e.g. a tree will have faster lookup times but is more tricky to get
   right, etc.

   R/

   On Sun, May 31, 2009 at 6:11 AM, kaloer mkal...@gmail.com wrote:

Okay, I'll look at it..
Thank you very much for your help

On 31 Maj, 14:59, Mark Murphy mmur...@commonsware.com wrote:
kaloer wrote:
 Well, of course I shouldn't. This only adds one line when it's 
 called.
 Should I call it before the while-loop begins?

You're right -- I skimmed it too quickly.

It's actually a bit more complicated than that. The flow is:

begin-transaction
insert 100 rows worth of stuff
set-transaction-successful

and do that whole block 1000 times for 100,000 words.

So you're probably going to wind up with something like:

while ((line=input.readLine())!=null) {
    DB.beginTransaction();
    DB.execSQL(...);

    for (int i=0;i99  (line=input.readLine())!=null; i++) {
        DB.execSQL(...);
    }

    DB.setTransactionSuccessful();

}

plus an appropriate try/catch in there.

However, bear in mind that this will still take a very long time, so
unless you're trying this for educational purposes, I'd move along to
one of the other options.

--
Mark Murphy (a Commons 
Guy)http://commonsware.com|http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 2.0 Available!


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Best way to add 100.000 words to database

2009-06-10 Thread Gavin Aiken
Hey,

In addition to what Mark said you might want to look at using a trie.

http://groups.google.com/group/android-developers/browse_thread/thread/e7857f4eaf2856fd/ff84d0a6bf64da83?lnk=gstq=trie#ff84d0a6bf64da83

A few of the other developers seem to be using this technique.

Gav


On Wed, Jun 10, 2009 at 2:27 PM, kaloer mkal...@gmail.com wrote:


 I think you are right. I'll look at it,
 Thank you very much

 //Kaloer

 On 2 Jun., 13:05, kaloer mkal...@gmail.com wrote:
  Btw, this is how the app works:
 
  There is a text view and a button. The user will enter a single word,
  and when he presses the button, the application checks whether this is
  actually a real word.
 
  On 2 Jun., 09:27, kaloer mkal...@gmail.com wrote:
 
   It is a list of english words, that shall be used as a spell checker.
   I have tried to use a txt file and put the data to a List, but this is
   too slow because it needs to read all the data. But Maybe I should
   create a table in the database for each letter (and every second, if
   necessary) , so that it doesn't need to read all the data.
 
   On 1 Jun., 18:28, Raphael r...@android.com wrote:
 
This begs to a different kind of question: do you really need to
 store
this data in an sqlite3 database?
 
There are other alternatives, it all depends on your data and how you
want to use it so you might want to help us here.
 
Example: you mention words, just not any string, so your data might
be a dictionary.
Then in this case you could simply use a binary file, in your own
format, packaged in your project/res/raw directory, which you can
retrieve withhttp://
 d.android.com/reference/android/content/res/Resources.html#ope...)
-- the file is added uncompressed to your APK but the APK itself is a
zip file, which is why you get an input stream. That also means if
 you
need direct random access you'll probably want to write it somewhere,
such as on the sdcard.
Make sure to write an index for fast lookup, for example the offset
 of
words starting by A, by B, etc. You will probably need to experiment
 a
bit to get a tradeof between simplicity of the index and lookup time,
e.g. a tree will have faster lookup times but is more tricky to get
right, etc.
 
R/
 
On Sun, May 31, 2009 at 6:11 AM, kaloer mkal...@gmail.com wrote:
 
 Okay, I'll look at it..
 Thank you very much for your help
 
 On 31 Maj, 14:59, Mark Murphy mmur...@commonsware.com wrote:
 kaloer wrote:
  Well, of course I shouldn't. This only adds one line when it's
 called.
  Should I call it before the while-loop begins?
 
 You're right -- I skimmed it too quickly.
 
 It's actually a bit more complicated than that. The flow is:
 
 begin-transaction
 insert 100 rows worth of stuff
 set-transaction-successful
 
 and do that whole block 1000 times for 100,000 words.
 
 So you're probably going to wind up with something like:
 
 while ((line=input.readLine())!=null) {
 DB.beginTransaction();
 DB.execSQL(...);
 
 for (int i=0;i99  (line=input.readLine())!=null; i++) {
 DB.execSQL(...);
 }
 
 DB.setTransactionSuccessful();
 
 }
 
 plus an appropriate try/catch in there.
 
 However, bear in mind that this will still take a very long time,
 so
 unless you're trying this for educational purposes, I'd move along
 to
 one of the other options.
 
 --
 Mark Murphy (a Commons Guy)http://commonsware.com|
 http://twitter.com/commonsguy
 
 _The Busy Coder's Guide to Android Development_ Version 2.0
 Available!
 
 
 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Best way to add 100.000 words to database

2009-06-02 Thread kaloer

It is a list of english words, that shall be used as a spell checker.
I have tried to use a txt file and put the data to a List, but this is
too slow because it needs to read all the data. But Maybe I should
create a table in the database for each letter (and every second, if
necessary) , so that it doesn't need to read all the data.

On 1 Jun., 18:28, Raphael r...@android.com wrote:
 This begs to a different kind of question: do you really need to store
 this data in an sqlite3 database?

 There are other alternatives, it all depends on your data and how you
 want to use it so you might want to help us here.

 Example: you mention words, just not any string, so your data might
 be a dictionary.
 Then in this case you could simply use a binary file, in your own
 format, packaged in your project/res/raw directory, which you can
 retrieve 
 withhttp://d.android.com/reference/android/content/res/Resources.html#ope...)
 -- the file is added uncompressed to your APK but the APK itself is a
 zip file, which is why you get an input stream. That also means if you
 need direct random access you'll probably want to write it somewhere,
 such as on the sdcard.
 Make sure to write an index for fast lookup, for example the offset of
 words starting by A, by B, etc. You will probably need to experiment a
 bit to get a tradeof between simplicity of the index and lookup time,
 e.g. a tree will have faster lookup times but is more tricky to get
 right, etc.

 R/

 On Sun, May 31, 2009 at 6:11 AM, kaloer mkal...@gmail.com wrote:

  Okay, I'll look at it..
  Thank you very much for your help

  On 31 Maj, 14:59, Mark Murphy mmur...@commonsware.com wrote:
  kaloer wrote:
   Well, of course I shouldn't. This only adds one line when it's called.
   Should I call it before the while-loop begins?

  You're right -- I skimmed it too quickly.

  It's actually a bit more complicated than that. The flow is:

  begin-transaction
  insert 100 rows worth of stuff
  set-transaction-successful

  and do that whole block 1000 times for 100,000 words.

  So you're probably going to wind up with something like:

  while ((line=input.readLine())!=null) {
      DB.beginTransaction();
      DB.execSQL(...);

      for (int i=0;i99  (line=input.readLine())!=null; i++) {
          DB.execSQL(...);
      }

      DB.setTransactionSuccessful();

  }

  plus an appropriate try/catch in there.

  However, bear in mind that this will still take a very long time, so
  unless you're trying this for educational purposes, I'd move along to
  one of the other options.

  --
  Mark Murphy (a Commons 
  Guy)http://commonsware.com|http://twitter.com/commonsguy

  _The Busy Coder's Guide to Android Development_ Version 2.0 Available!
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Best way to add 100.000 words to database

2009-06-02 Thread kaloer

Btw, this is how the app works:

There is a text view and a button. The user will enter a single word,
and when he presses the button, the application checks whether this is
actually a real word.

On 2 Jun., 09:27, kaloer mkal...@gmail.com wrote:
 It is a list of english words, that shall be used as a spell checker.
 I have tried to use a txt file and put the data to a List, but this is
 too slow because it needs to read all the data. But Maybe I should
 create a table in the database for each letter (and every second, if
 necessary) , so that it doesn't need to read all the data.

 On 1 Jun., 18:28, Raphael r...@android.com wrote:

  This begs to a different kind of question: do you really need to store
  this data in an sqlite3 database?

  There are other alternatives, it all depends on your data and how you
  want to use it so you might want to help us here.

  Example: you mention words, just not any string, so your data might
  be a dictionary.
  Then in this case you could simply use a binary file, in your own
  format, packaged in your project/res/raw directory, which you can
  retrieve 
  withhttp://d.android.com/reference/android/content/res/Resources.html#ope...)
  -- the file is added uncompressed to your APK but the APK itself is a
  zip file, which is why you get an input stream. That also means if you
  need direct random access you'll probably want to write it somewhere,
  such as on the sdcard.
  Make sure to write an index for fast lookup, for example the offset of
  words starting by A, by B, etc. You will probably need to experiment a
  bit to get a tradeof between simplicity of the index and lookup time,
  e.g. a tree will have faster lookup times but is more tricky to get
  right, etc.

  R/

  On Sun, May 31, 2009 at 6:11 AM, kaloer mkal...@gmail.com wrote:

   Okay, I'll look at it..
   Thank you very much for your help

   On 31 Maj, 14:59, Mark Murphy mmur...@commonsware.com wrote:
   kaloer wrote:
Well, of course I shouldn't. This only adds one line when it's called.
Should I call it before the while-loop begins?

   You're right -- I skimmed it too quickly.

   It's actually a bit more complicated than that. The flow is:

   begin-transaction
   insert 100 rows worth of stuff
   set-transaction-successful

   and do that whole block 1000 times for 100,000 words.

   So you're probably going to wind up with something like:

   while ((line=input.readLine())!=null) {
       DB.beginTransaction();
       DB.execSQL(...);

       for (int i=0;i99  (line=input.readLine())!=null; i++) {
           DB.execSQL(...);
       }

       DB.setTransactionSuccessful();

   }

   plus an appropriate try/catch in there.

   However, bear in mind that this will still take a very long time, so
   unless you're trying this for educational purposes, I'd move along to
   one of the other options.

   --
   Mark Murphy (a Commons 
   Guy)http://commonsware.com|http://twitter.com/commonsguy

   _The Busy Coder's Guide to Android Development_ Version 2.0 Available!
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Best way to add 100.000 words to database

2009-06-02 Thread Mark Murphy

kaloer wrote:
 Btw, this is how the app works:
 
 There is a text view and a button. The user will enter a single word,
 and when he presses the button, the application checks whether this is
 actually a real word.

You might wish to do some research on how spell checkers and
dictionaries are traditionally implemented. I would be rather surprised
if many used a SQL database.

For example:

http://jaspell.sourceforge.net/

They use ternary search trees for storing their dictionary. Moreover,
they have an implementation of such trees in Java under a BSD license.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android App Developer Books: http://commonsware.com/books.html

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Best way to add 100.000 words to database

2009-06-01 Thread Raphael

This begs to a different kind of question: do you really need to store
this data in an sqlite3 database?

There are other alternatives, it all depends on your data and how you
want to use it so you might want to help us here.

Example: you mention words, just not any string, so your data might
be a dictionary.
Then in this case you could simply use a binary file, in your own
format, packaged in your project/res/raw directory, which you can
retrieve with 
http://d.android.com/reference/android/content/res/Resources.html#openRawResource(int)
-- the file is added uncompressed to your APK but the APK itself is a
zip file, which is why you get an input stream. That also means if you
need direct random access you'll probably want to write it somewhere,
such as on the sdcard.
Make sure to write an index for fast lookup, for example the offset of
words starting by A, by B, etc. You will probably need to experiment a
bit to get a tradeof between simplicity of the index and lookup time,
e.g. a tree will have faster lookup times but is more tricky to get
right, etc.

R/

On Sun, May 31, 2009 at 6:11 AM, kaloer mkal...@gmail.com wrote:

 Okay, I'll look at it..
 Thank you very much for your help


 On 31 Maj, 14:59, Mark Murphy mmur...@commonsware.com wrote:
 kaloer wrote:
  Well, of course I shouldn't. This only adds one line when it's called.
  Should I call it before the while-loop begins?

 You're right -- I skimmed it too quickly.

 It's actually a bit more complicated than that. The flow is:

 begin-transaction
 insert 100 rows worth of stuff
 set-transaction-successful

 and do that whole block 1000 times for 100,000 words.

 So you're probably going to wind up with something like:

 while ((line=input.readLine())!=null) {
     DB.beginTransaction();
     DB.execSQL(...);

     for (int i=0;i99  (line=input.readLine())!=null; i++) {
         DB.execSQL(...);
     }

     DB.setTransactionSuccessful();

 }

 plus an appropriate try/catch in there.

 However, bear in mind that this will still take a very long time, so
 unless you're trying this for educational purposes, I'd move along to
 one of the other options.

 --
 Mark Murphy (a Commons 
 Guy)http://commonsware.com|http://twitter.com/commonsguy

 _The Busy Coder's Guide to Android Development_ Version 2.0 Available!
 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Best way to add 100.000 words to database

2009-05-31 Thread Mark Murphy

kaloer wrote:
 Hi,
 I am developing an application that, as the first thing, puts all
 lines from a txt-file to a sqlite database. There is about 100.000
 words that should be added to the database, and this takes almost an
 hour to do.
 Is there a better way to do this, fx. to have a database in the res/
 raw resources if it is possible.
 This is my currently way to do it:
 
 public void getWords(final Context context, final InputStream aFile) {
 try {
 InputStreamReader reader = new InputStreamReader(aFile,
 Charset.forName(UTF-8));
 BufferedReader input =  new BufferedReader(reader);
 try {
   DB = context.openOrCreateDatabase(DATABASE_NAME, 1, 
 null);
   DB.execSQL(DELETE FROM english);
   String line = input.readLine();
   int i = 0;
   while (( line = input.readLine()) != null){
   try {
   DB.execSQL(INSERT OR IGNORE INTO english(word) 
 VALUES
 (' + line + '));
   }
   catch (Exception e) {
   Log.e(kaloer, e.getMessage());
   }
   if(i % 100 == 0) {
   Log.d(progress, 
 Integer.toString(i));
   try {
   Thread.sleep(100);
   } 
 catch(InterruptedException e) {
   }
   }
   i++;
   }
 } catch (IOException e) {
   e.printStackTrace();
   }
 finally {
try {
   input.close();
   } catch (IOException e) {
   e.printStackTrace();
   }
DB.close();
   }
   }
 return;
 
 }

1. Right now, you are doing 100,000 transactions. Wrap your own
transaction around the INSERT loop, one for every 1,000 words or something.

2. Create the SQLite database on your development PC and package it as a
raw resource or asset. Then, before you need it, copy it from the
resource to the appropriate databases/ directory by reading from a raw
resource or asset stream. Downside #1: if SQLite ever changes its
database format, you're in trouble. Downside #2: you take up double the
memory.

3. Create the SQLite database on your development PC and download it off
the Internet before first use. Downside: if SQLite ever changes its
database format, you're in trouble.

4. Use some other data structure and library than SQLite.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 2.0 Available!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Best way to add 100.000 words to database

2009-05-31 Thread kaloer

How does the transaction work? I'm new to databases. I found this on
the android documentation

   db.beginTransaction();
   try {
 ...
 db.setTransactionSuccessful();
   } finally {
 db.endTransaction();
   }

Should I implement it like this:


int i = 0;
while (( line = input.readLine()) != null){
if(i % 100 == 0) {
   DB.beginTransaction();
   Try {
DB.execSQL(INSERT OR IGNORE INTO
english(word) VALUES
DB.setTransactionSuccessfull
();
} Finally {
DB.endTransaction();
   }

On 31 Maj, 14:13, Mark Murphy mmur...@commonsware.com wrote:
 kaloer wrote:
  Hi,
  I am developing an application that, as the first thing, puts all
  lines from a txt-file to a sqlite database. There is about 100.000
  words that should be added to the database, and this takes almost an
  hour to do.
  Is there a better way to do this, fx. to have a database in the res/
  raw resources if it is possible.
  This is my currently way to do it:

  public void getWords(final Context context, final InputStream aFile) {
          try {
               InputStreamReader reader = new InputStreamReader(aFile,
  Charset.forName(UTF-8));
               BufferedReader input =  new BufferedReader(reader);
               try {
                     DB = context.openOrCreateDatabase(DATABASE_NAME, 1, 
  null);
                     DB.execSQL(DELETE FROM english);
                 String line = input.readLine();
                 int i = 0;
                 while (( line = input.readLine()) != null){
                     try {
                             DB.execSQL(INSERT OR IGNORE INTO english(word) 
  VALUES
  (' + line + '));
                     }
                     catch (Exception e) {
                             Log.e(kaloer, e.getMessage());
                                     }
                     if(i % 100 == 0) {
                                             Log.d(progress, 
  Integer.toString(i));
                                             try {
                                                     Thread.sleep(100);
                                                     } 
  catch(InterruptedException e) {
                                                     }
                             }
                     i++;
                 }
               } catch (IOException e) {
                             e.printStackTrace();
                     }
               finally {
                      try {
                                     input.close();
                             } catch (IOException e) {
                                     e.printStackTrace();
                             }
                      DB.close();
                     }
             }
          return;

      }

 1. Right now, you are doing 100,000 transactions. Wrap your own
 transaction around the INSERT loop, one for every 1,000 words or something.

 2. Create the SQLite database on your development PC and package it as a
 raw resource or asset. Then, before you need it, copy it from the
 resource to the appropriate databases/ directory by reading from a raw
 resource or asset stream. Downside #1: if SQLite ever changes its
 database format, you're in trouble. Downside #2: you take up double the
 memory.

 3. Create the SQLite database on your development PC and download it off
 the Internet before first use. Downside: if SQLite ever changes its
 database format, you're in trouble.

 4. Use some other data structure and library than SQLite.

 --
 Mark Murphy (a Commons 
 Guy)http://commonsware.com|http://twitter.com/commonsguy

 _The Busy Coder's Guide to Android Development_ Version 2.0 Available!
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Best way to add 100.000 words to database

2009-05-31 Thread Mark Murphy

kaloer wrote:
 How does the transaction work? I'm new to databases. I found this on
 the android documentation
 
db.beginTransaction();
try {
  ...
  db.setTransactionSuccessful();
} finally {
  db.endTransaction();
}
 
 Should I implement it like this:
 
 
 int i = 0;
 while (( line = input.readLine()) != null){
 if(i % 100 == 0) {
DB.beginTransaction();
Try {
 DB.execSQL(INSERT OR IGNORE INTO
 english(word) VALUES
 DB.setTransactionSuccessfull
 ();
 } Finally {
 DB.endTransaction();
}
 

Something like that should work. This will cut the time somewhat, but
100,000 insert statements will still take a fair amount of time to
process, coarser transactions or not.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 2.0 Available!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Best way to add 100.000 words to database

2009-05-31 Thread kaloer

Well, of course I shouldn't. This only adds one line when it's called.
Should I call it before the while-loop begins?

On 31 Maj, 14:47, kaloer mkal...@gmail.com wrote:
 How does the transaction work? I'm new to databases. I found this on
 the android documentation

    db.beginTransaction();
    try {
      ...
      db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
    }

 Should I implement it like this:

                     int i = 0;
                     while (( line = input.readLine()) != null){
                         if(i % 100 == 0) {
                                DB.beginTransaction();
                                Try {
                                 DB.execSQL(INSERT OR IGNORE INTO
 english(word) VALUES
                                         DB.setTransactionSuccessfull
 ();
                                 } Finally {
                                         DB.endTransaction();
                                }

 On 31 Maj, 14:13, Mark Murphy mmur...@commonsware.com wrote:

  kaloer wrote:
   Hi,
   I am developing an application that, as the first thing, puts all
   lines from a txt-file to a sqlite database. There is about 100.000
   words that should be added to the database, and this takes almost an
   hour to do.
   Is there a better way to do this, fx. to have a database in the res/
   raw resources if it is possible.
   This is my currently way to do it:

   public void getWords(final Context context, final InputStream aFile) {
           try {
                InputStreamReader reader = new InputStreamReader(aFile,
   Charset.forName(UTF-8));
                BufferedReader input =  new BufferedReader(reader);
                try {
                      DB = context.openOrCreateDatabase(DATABASE_NAME, 1, 
   null);
                      DB.execSQL(DELETE FROM english);
                  String line = input.readLine();
                  int i = 0;
                  while (( line = input.readLine()) != null){
                      try {
                              DB.execSQL(INSERT OR IGNORE INTO 
   english(word) VALUES
   (' + line + '));
                      }
                      catch (Exception e) {
                              Log.e(kaloer, e.getMessage());
                                      }
                      if(i % 100 == 0) {
                                              Log.d(progress, 
   Integer.toString(i));
                                              try {
                                                      Thread.sleep(100);
                                                      } 
   catch(InterruptedException e) {
                                                      }
                              }
                      i++;
                  }
                } catch (IOException e) {
                              e.printStackTrace();
                      }
                finally {
                       try {
                                      input.close();
                              } catch (IOException e) {
                                      e.printStackTrace();
                              }
                       DB.close();
                      }
              }
           return;

       }

  1. Right now, you are doing 100,000 transactions. Wrap your own
  transaction around the INSERT loop, one for every 1,000 words or something.

  2. Create the SQLite database on your development PC and package it as a
  raw resource or asset. Then, before you need it, copy it from the
  resource to the appropriate databases/ directory by reading from a raw
  resource or asset stream. Downside #1: if SQLite ever changes its
  database format, you're in trouble. Downside #2: you take up double the
  memory.

  3. Create the SQLite database on your development PC and download it off
  the Internet before first use. Downside: if SQLite ever changes its
  database format, you're in trouble.

  4. Use some other data structure and library than SQLite.

  --
  Mark Murphy (a Commons 
  Guy)http://commonsware.com|http://twitter.com/commonsguy

  _The Busy Coder's Guide to Android Development_ Version 2.0 Available!
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Best way to add 100.000 words to database

2009-05-31 Thread Mark Murphy

kaloer wrote:
 Well, of course I shouldn't. This only adds one line when it's called.
 Should I call it before the while-loop begins?

You're right -- I skimmed it too quickly.

It's actually a bit more complicated than that. The flow is:

begin-transaction
insert 100 rows worth of stuff
set-transaction-successful

and do that whole block 1000 times for 100,000 words.

So you're probably going to wind up with something like:

while ((line=input.readLine())!=null) {
DB.beginTransaction();
DB.execSQL(...);

for (int i=0;i99  (line=input.readLine())!=null; i++) {
DB.execSQL(...);
}

DB.setTransactionSuccessful();
}

plus an appropriate try/catch in there.

However, bear in mind that this will still take a very long time, so
unless you're trying this for educational purposes, I'd move along to
one of the other options.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 2.0 Available!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Best way to add 100.000 words to database

2009-05-31 Thread kaloer

Okay, I'll look at it..
Thank you very much for your help


On 31 Maj, 14:59, Mark Murphy mmur...@commonsware.com wrote:
 kaloer wrote:
  Well, of course I shouldn't. This only adds one line when it's called.
  Should I call it before the while-loop begins?

 You're right -- I skimmed it too quickly.

 It's actually a bit more complicated than that. The flow is:

 begin-transaction
 insert 100 rows worth of stuff
 set-transaction-successful

 and do that whole block 1000 times for 100,000 words.

 So you're probably going to wind up with something like:

 while ((line=input.readLine())!=null) {
     DB.beginTransaction();
     DB.execSQL(...);

     for (int i=0;i99  (line=input.readLine())!=null; i++) {
         DB.execSQL(...);
     }

     DB.setTransactionSuccessful();

 }

 plus an appropriate try/catch in there.

 However, bear in mind that this will still take a very long time, so
 unless you're trying this for educational purposes, I'd move along to
 one of the other options.

 --
 Mark Murphy (a Commons 
 Guy)http://commonsware.com|http://twitter.com/commonsguy

 _The Busy Coder's Guide to Android Development_ Version 2.0 Available!
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---