In this tutorial I’ll continue making my Android SQLite Address Book app. I hope this code is useful enough to use as a cheat sheet when you need to use an SQLite database in Android.
I cover a ton of topics: How to Create a SQLite Database and Tables, How to Issue Queries, How to Insert Data, How to Update Data, How to Delete Data, SQLiteOpenHelper, execSQL, SQLiteDatabase, ContentValues, rawQuery, Cursor, and more. The code below will help.
If you like tutorials like this, it helps to tell Google+ with a click here [googleplusone]
Code From the Video
DBTools.java
Hello Derek. Thanks for the beautiful tutorials.
Just want to pay your attention to a small issue about auto_complete in eclipse. When you want to type for example
android:textColor=""you can start fromteand press ctrl+space and it will give a list of usefull suggestions.Another example:
android:text="@string/add_contact– Type
tethen ctrl+space forandroid:text=β"– Type
@sthen ctrl+space for@string/– Type
adthen ctrl+space foradd_contactThis is for windows. I donβt know what is the equivalent in Mac.
Thank you.
There are many short-cuts. Just few more:
For
android:layout_weight="1"Just type
wthen Ctrl+Space and it will give you a list of suggestions,so that you can easily pick it out.For
android:padding="@dimen/pendding_5dp"/>– Type
pathen Ctrl+Space forandroid:padding=""– Type
@dthen Ctrl+Space for@dimen/– Type
pthen Ctrl+Space forpendding_5dpHope that help you instead of jumping from place to place to copy things, or jump to Graphical layout just for the purpose of searching for a string value.
Thank you for the great tips π
Thank you for the great tips π I very much appreciate them!
Wow, the range of things you are covering with this ContactsApp is great, learning so much in just this 5 part series alone!
I’m a bit of a slow learner but the way you present this is brilliant. Derek, thanks x 100 for the wealth of information on your site, and for sharing your expertise!
Thank you π Yes I’m doing my best to cover as many topics as possible in each app I make. I’m glad you are finding them useful
Thank you for this nice tutorial.:)
You’re very welcome π
One more question: why do we need to close the database after an insert operation but not after a delete?
-Jay
You should actually close it in both situations, but since this was a very simple app I didn’t run into any issues.
Hi Derek, I managed to solve the problems I had with the interface and the positioning of the buttons. I just uninstalled and jdk for Eclipes reinstalled following the tutorial Install Android Development Tools. However now I have a problem, for the tutorial 12 I am not able to make a file that is named DBTools.java. Will I need to install additional plug-in?
I have all of the code in this one package. If you import it into Eclipse it will be easy to see how everything is laid out. I hope that helps π
Great tutorials, I can give you another shortcut tip.
When you have to import any library just press: CTRL + SHIFT + O(the letter), and if you want to format and indent your code just press: CTRL + SHIFT + F.
Have a great day!
Thank you for the tips. I know about the shortcuts, but I decided not to use them in tutorials because I can never trust that anyone else knows them. I hope that makes sense π
Hi Derek,
Have you worked on using a pre-populated database e.g. android.db is your database name. You created all the tables and inserted data using SQLite Database browser or from command line. You saved it to assets/android.db directory. From there, your program can copy it over to android database location /data/data//database/.
I tried the solution from the following article, however, I was not successful. It always fails because a database table already exists. Do you know how to do this? Please share your solution.
http://blog.softeq.com/2012/12/using-pre-populated-sqlite-database-in.html
Thanks a lot!
Hi,
You basically just save your database to the assets folder in your Android project. Then just use it like it was created in your app.
How do you call a database you’ve already created? And where do you put it in your app? Thank you in advance.
Here you go http://stackoverflow.com/questions/6540906/android-simple-export-and-import-of-sqlite-database
Hi Derek,
Thanks a lot for these tutorials, they are really much more helpful than any book on the topic that i have read. I tend to get bored very fast with java books, they simply contain to much nonsense between the lines. Actually, the only good programming book that i have read were about Phyton (Python Programming for the Absolute Beginner). In each chapter you creat a program from start to finish, with no unanswered questions after the chapter. Similar to what you are doing with your tutorials! Ive been looking for such a java book for years now, and 6 java books later i found your tutorials by chance. The only regret is that i were trying so long with the books.
Keep up the good work!
Hi Oskar,
Thank you for the very nice compliments π I’m making a new Android tutorial as well very soon.
What’s a hashmap how it works.
Here is a tutorial on Hash tables which are similar.
Well I see I didn’t got it completely but why did you created DBTools.java in other project than the Address Book Application? Can I create it in the later?
Tip : Do add the next video page link on your website. For example – there should be a link on this page pointing to Part 13 page.
I always try to separate out any classes that I can to keep everything modular. I have every Android video on this page Android video tutorial