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 -~----------~----~----~----~------~----~------~--~---