Posts

Showing posts with the label SQLite

Loading data into an android database

Image
AutoCompleteTextView I wanted to demonstrate how it is possible to back an AutoCompleteTextView  with data from a database Cursor. To do this I needed to put a few thousand rows of data into the SQLite database on the device. I got my list of words here: http://www.wordlistgenerator.net/ I just left it with the default options to give me a nice long list. I took the apostrophes out to avoid having to escape them - I am only looking for semi junk input afterall. Location of 'words' file I saved the list into a file called 'words'. Note that there is no extension here. I am going to use this as a resource in Android and the extension interferes with this. I dropped the file into my res/drawable folder (as good as any) in order to be able to access it with my load method below. Then I used the following code to load the rows from words into the database: public void load() { SQLiteDatabase db = getReadableDatabase(); try { InputStream is =...

AutoCompleteTextView backed with data from SQLite in Android

Image
This is a handy piece of code. As your database gets bigger you can allow the user to pick the correct value by typing. If the db gets very big, you can always increase the threshold, so that the query does not run until there will be a suitably sized resultset returned. Running it at a threshold of 1 is fine for very small resultsets.  Here is the code you need to back your AutoCompleteTextView with a cursor adapter. @Override protected void onResume() { super.onResume(); text = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); final AdapterHelper h = new AdapterHelper(this); Cursor c = h.getAllResults(); startManagingCursor(c); String[] from = new String[] { "val" }; int[] to = new int[] { android.R.id.text1 }; CursorAdapter adapter = new MyCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, c, from, to); adapter.setFilterQueryProvider(new FilterQueryProvider() { public Cursor runQuery(Cha...

Date comparisons in SQLite on Android

Image
I have been tripped up a few times using date comparisons in SQLite. This is where I pass in a correctly formatted date string and want rows from the database which are equal to this for example. I get this to work in a couple of steps: 1. Every date I put in the database goes through this method. It tries to parse the date according to a pattern I have defined and if the date wont parse, returns a null. public static String dateForDb(String date) { String formattedDate = null; try { Date d = null; d = DateFactory.SDF_SHORT.parse(date); if (d != null) { formattedDate = DateFactory.SDF_SHORT.format(d); } } catch (Exception e) { e.printStackTrace(); } return formattedDate; } This ensures that everything that gets into my database is correctly formatted for the next bit. 2. In my sql statement I take out the date = string stuff and replace it with a static call to this method: public static String dbDateCompare(St...

Google Maps for Android

Made good progress on this today. I had fiddled with it a bit last year, but had not gotten around to it for a long time and expected all to be lost, but not so. This is roughly the learning approach that I used: Here is the Google page for location and maps. I worked along with the simple example and soon got it working. Make sure GPS is enabled on your emulator (if you are using one) - think this is the default, but you never know what the story will be with other emulator packages. Next is Hello, MapView. Same drill, work through it and bit by bit it gets ironed out. Then I reviewed the documentation on the Google API for Android. All of these classes concern maps, but there is supposed to be other stuff to the APIs. Not sure how this works out. Ok, maybe it was not that linear, a bit of scrambling all over the place at the beginning until I knew where I was. I am paranoid about leaving sensors and other juice gobblers running inadvertently, so I took some time to go t...

Getting started with charting

I started using the  http://www.achartengine.org  library in my app today. Have not integrated it properly yet. So far I have just shoved the demo activity into my existing app. This is the jar and what goes where part of the job out of the way, so now I just have to get the hang of the renderers and pick a nice looking chart. More on this as I use it, but so far it looks nice. Tons of places to use charts in just about any app. Fixed a bunch of problems related to how activities are kicked off when you are expecting a result. Using startActivityForResult and calling finish from the target Activity means that your user does not end up meandering through no longer relevant Activities as they press the back button. The overall flow is now much improved. Still getting tripped up by the upgrade path for a modified database. I reckon this should be a straightforward, almost mechanical method to write (the override of SQLiteOpenHelper.onUpgrade()), but I keep getting knotte...

Slow progress and fighting with sqlite

Second day at the (home) office was harder than the first. Fiddling with queries on sqlite took up a lot of the day. I have been doing sql for years, but I think I will just have to test the stuff out more thoroughly before I put it into an android program. The cycle between develop, deploy and test is too long and fiddly to be working out why your where clause is eliminating all of your rows. Outer join - I added an outer join to my code today. Not exactly rocket science, but very handy all the same. The app I am developing allows people pick 30 day challenges. They enter their progress against these items as they go along. It is supposed to be a way to get into new, productive habits. I got the idea here: http://www.youtube.com/watch?v=JnfBXjWm7hc Its early days yet, but I like these kind of apps, so it seemed like as good a place as any to start. The outer join is useful for pulling back the tasks that the user has in their list. It is outer as I want to show them the ones th...

Working with SQLite in Android. Tools and stuff.

The Notepad tutorial uses sqlite to store the users entered notes. This is a single table, so this method query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)  was fine. It takes a single table name and some params for the other clauses. To use more than one table I used the rawQuery(String sql, String[] selectionArgs) method. This takes arbitrary sql (union in my case) along with values to bind to any ? that you used. I have removed the SQLiteOpenHelper from the code also. This opens and creates your database and handles upgrading the database following schema changes. Overall I found this cumbersome and have switched to moving the database file from the device (emulator) to my pc. I then query it using SQLite 2009 Pro. I got it here . Excellent query analyzer type tool. I make my changes here (schema and data) and then push this back to the device when I am ready to test with the app. The pushing is...

Using SQLite with the Android emulator and Eclipse

One of the ways of persisting data with Android is to use a SQLite database. A fuller list of methods is given here . When you are persisting data in any application one of the things you will want to do is write ad hoc queries to see is you data getting to the database, how big are your tables etc. SQLLite lets you do this with the databases that your applications use in the emulator. You will need to connect to the emulator using the Android Debug Bridge (adb). Type this at a command prompt (you obviously need to have adb.exe on your path, or just open a command prompt in the tools directory of your android installation - \android-sdk-windows\tools in my case): adb -s emulator-5554 shell The name of your emulator may be different, you can check the name in the console of eclipse when you are launching an app to run on the emulator. Once you have run that you will need to use the sqlite3 command to mount the database you are interested in. Enter this at the shell prompt which...