0

I am working on an Android application, where some of the functions that users should input: register, locate hospital location, search for messages (sent via sms to registered users) and can schedule and make appointments.

So far, all the activities are done. However, I need to know how to connect to the database in order to read and store this data. I am using Android 2.1, Mac OS X, and the SQLite Firefox extension.

This is the code I have to connect to the SQLite database, what am I doing wrong?

package com.bitnuts.imom;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbManager extends SQLiteOpenHelper {

    private static final String DB_NAME = "iMom.sqlite";
    private static final String DB_PATH = "/data/data/com.bitnuts.imom/databases/";
    private static final Integer DB_VERSION = 1;
    private static final String TAG = "DbManager";
    private final Context context;
    private SQLiteDatabase db;
    private DbManager dbManager;

    public DbManager(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.context = context;
    }

    public DbManager open() {
        dbManager = new DbManager(context);
        db = dbManager.getWritableDatabase();
        return this;
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {

        createNewDatabase();
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

        // TODO Auto-generated method stub
    }

    public void createNewDatabase() {
        InputStream assetsDB = null;
        try {
            assetsDB = context.getAssets().open(DB_NAME);
            OutputStream dbOut = new FileOutputStream(DB_PATH + DB_NAME);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = assetsDB.read(buffer)) > 0) {
                dbOut.write(buffer, 0, length);
            }

            dbOut.flush();
            dbOut.close();
            assetsDB.close();
        }
        catch (IOException e) {
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kinyanjui Kamau
  • 1,890
  • 10
  • 55
  • 95

3 Answers3

2

There is no JDBC driver on Android, so you can not connect to databases on the network (SQLite or other): Android - Access to online Database SQlite

Android comes with an embedded SQLite database, but this is available only within the device.

Also, read why direct connection to databases from mobile devices is not recommended.

If you need to store/share some data on the network, then the usual way is to create a web service on the server that all devices can connect to. A very popular type of web service is REST, which is quite easy to set up in Java.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Thanks for this answer. I am a student, and i am running this on the android emulator. I do not have anything on an outside server yet. Do i still need a REST layer to be able to interact with a database on localhost, or is there another way? – Kinyanjui Kamau Feb 15 '11 at 08:40
  • There are no database drivers on Android, so you can not connect to databases (except for the embedded database on device). So, yes, you need some kind of web services layer to connect to, REST being one of more popular. – Peter Knego Feb 15 '11 at 11:38
2

You cannot use the SQLite database directly in your application. You need to make some modifications. Such as the android_metadata table, etc. The blog post Using your own SQLite database in Android applications will help you.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tushar Vengurlekar
  • 7,649
  • 8
  • 33
  • 48
0

I suggest to use ORMLite for Android. The implementation becomes a lot simpler with it. Here's the getting started documentation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brij Raj Singh - MSFT
  • 4,903
  • 7
  • 36
  • 55